Skip to content
This repository was archived by the owner on Jun 20, 2025. It is now read-only.

Commit 980daff

Browse files
Ajinkya Ghongefacebook-github-bot
authored andcommitted
Add classes to parse PC Instruction set. (#2268)
Summary: Pull Request resolved: #2268 # Context As per PC Translator design, we need a runtime library will be called during PC run. This library will be called at the beginning of PC run to encode specified fields in publisher side input into a encoded breakdown (aggregation) Ids based on active PC instruction sets for the run. The library will filter the active PC Instruction sets for the run based on parsing the pcs_features i.e. gatekeepers for the particular run. # Product decisions In this stack we would focus solely on functionality required for private lift runs. We would focus on the MVP implementation of the library and its integration with fbpcf ORAM encoder library in this stack. # Stack 1. Create runtime pc_translator library. 2. Add logic to retrieve and parse PC instruction set, filtered based on the active gatekeepers for the run. 3. Integrate pc_translator library with fbpcf ORAM encoder. 4. Add logic to generate transformed publisher output with encoded breakdown ID and write the output. # In this diff Add logic to retrieve and parse PC instruction set. Differential Revision: D44618035 Privacy Context Container: L416713 fbshipit-source-id: 82c924ad89e85b9d5c16db8ff59b74a639499dc8
1 parent 56cb4a1 commit 980daff

File tree

8 files changed

+324
-17
lines changed

8 files changed

+324
-17
lines changed

fbpcs/pc_translator/PCTranslator.cpp

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,70 @@
66
*/
77

88
#include "fbpcs/pc_translator/PCTranslator.h"
9+
#include "fbpcs/pc_translator/input_processing/PCInstructionSet.h"
10+
11+
#include <fbpcf/common/FunctionalUtil.h>
12+
#include <fbpcf/io/api/FileIOWrappers.h>
13+
#include <set>
14+
#include "folly/String.h"
915

1016
namespace pc_translator {
1117

12-
std::string PCTranslator::encode(const std::string& /* inputDataset */) {
13-
throw std::runtime_error("Unimplemented");
18+
std::string PCTranslator::encode(const std::string& inputDataset) {
19+
auto validInstructionSetNames =
20+
PCTranslator::retrieveInstructionSetNamesForRun(pcsFeatures_);
21+
auto pcInstructionSets =
22+
PCTranslator::retrieveInstructionSets(validInstructionSetNames);
23+
PCTranslator::transformDataset(inputDataset, pcInstructionSets);
24+
return "";
1425
}
1526

1627
std::string PCTranslator::decode(
1728
const std::string& /* aggregatedOutputDataset */) {
1829
throw std::runtime_error("Unimplemented");
1930
}
2031

21-
void PCTranslator::retrieveInstructionSets(
22-
std::vector<std::string>& /* instructionSetNames */) {
23-
throw std::runtime_error("Unimplemented");
32+
std::vector<std::shared_ptr<PCInstructionSet>>
33+
PCTranslator::retrieveInstructionSets(
34+
std::vector<std::string>& instructionSetNames) {
35+
std::vector<std::shared_ptr<PCInstructionSet>> pcInstructionSets;
36+
for (auto instructionSetName : instructionSetNames) {
37+
auto file_path = instructionSetBasePath + instructionSetName + ".json";
38+
auto contents = fbpcf::io::FileIOWrappers::readFile(file_path);
39+
pcInstructionSets.push_back(PCTranslator::parseInstructionSet(contents));
40+
}
41+
return pcInstructionSets;
2442
}
2543

2644
std::vector<std::string> PCTranslator::retrieveInstructionSetNamesForRun(
27-
const std::string& /* pcsFeatures */) {
28-
throw std::runtime_error("Unimplemented");
45+
const std::string& pcsFeatures) {
46+
std::set<std::string> enabledFeatureFlags;
47+
folly::splitTo<std::string>(
48+
',',
49+
pcsFeatures,
50+
std::inserter(enabledFeatureFlags, enabledFeatureFlags.begin()),
51+
true);
52+
53+
std::vector<std::string> validPCInstructionSets;
54+
std::copy_if(
55+
enabledFeatureFlags.begin(),
56+
enabledFeatureFlags.end(),
57+
std::back_inserter(validPCInstructionSets),
58+
[](const std::string& feature) { return feature.find("pc_instr") == 0; });
59+
60+
return validPCInstructionSets;
2961
}
3062

31-
void PCTranslator::transformDataset(const std::string& /* input */) {
63+
void PCTranslator::transformDataset(
64+
const std::string& /* inputData */,
65+
const std::vector<std::shared_ptr<pc_translator::PCInstructionSet>>&
66+
pcInstructionSets) {
3267
throw std::runtime_error("Unimplemented");
3368
}
3469

35-
void PCTranslator::parseInstructionSet(
36-
const std::string& /* instructionSet */) {
37-
throw std::runtime_error("Unimplemented");
70+
std::shared_ptr<PCInstructionSet> PCTranslator::parseInstructionSet(
71+
const std::string& instructionSet) {
72+
return std::make_shared<PCInstructionSet>(PCInstructionSet::fromDynamic(
73+
folly::parseJson(std::move(instructionSet))));
3874
}
3975
} // namespace pc_translator

fbpcs/pc_translator/PCTranslator.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <memory>
1212
#include <string>
1313
#include <vector>
14+
#include "fbpcs/pc_translator/input_processing/PCInstructionSet.h"
1415

1516
namespace pc_translator {
1617

@@ -23,7 +24,7 @@ namespace pc_translator {
2324
class PCTranslator {
2425
public:
2526
explicit PCTranslator(const std::string& pcsFeatures)
26-
: pcsfeatures_(pcsFeatures) {}
27+
: pcsFeatures_(pcsFeatures) {}
2728

2829
/*
2930
* Method to encode the configurable fields in input dataset as per the active
@@ -41,12 +42,19 @@ class PCTranslator {
4142
std::string decode(const std::string& aggregatedOutputDataset);
4243

4344
private:
44-
std::string pcsfeatures_;
45-
void retrieveInstructionSets(std::vector<std::string>& instructionSetNames);
45+
std::string pcsFeatures_;
46+
const std::string instructionSetBasePath =
47+
"https://pc-translator.s3.us-west-2.amazonaws.com/";
48+
std::vector<std::shared_ptr<PCInstructionSet>> retrieveInstructionSets(
49+
std::vector<std::string>& instructionSetNames);
4650
std::vector<std::string> retrieveInstructionSetNamesForRun(
47-
const std::string& pcsfeatures);
48-
void parseInstructionSet(const std::string& instructionSet);
49-
void transformDataset(const std::string& input);
51+
const std::string& pcsFeatures);
52+
std::shared_ptr<PCInstructionSet> parseInstructionSet(
53+
const std::string& instructionSet);
54+
void transformDataset(
55+
const std::string& input_data,
56+
const std::vector<std::shared_ptr<pc_translator::PCInstructionSet>>&
57+
pcInstructionSets);
5058
};
5159

5260
} // namespace pc_translator
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#include "fbpcs/pc_translator/input_processing/FilterConstraint.h"
9+
10+
#include <cstdint>
11+
#include <memory>
12+
#include <string>
13+
#include <vector>
14+
15+
namespace pc_translator {
16+
FilterConstraint::FilterConstraint(
17+
const std::string& name,
18+
const std::string& type,
19+
int value)
20+
: name_(name), type_(type), value_(value) {}
21+
22+
std::string FilterConstraint::getName() const {
23+
return name_;
24+
}
25+
26+
std::string FilterConstraint::getType() const {
27+
return type_;
28+
}
29+
30+
int FilterConstraint::getValue() const {
31+
return value_;
32+
}
33+
} // namespace pc_translator
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#pragma once
9+
10+
#include <cstdint>
11+
#include <memory>
12+
#include <string>
13+
#include <vector>
14+
15+
namespace pc_translator {
16+
17+
/*
18+
* Class to store each filter constraint include in the PC instruction set.
19+
*/
20+
class FilterConstraint {
21+
public:
22+
FilterConstraint(const std::string& name, const std::string& type, int value);
23+
24+
/*
25+
* Name of the filter constraint i.e. the field on which this filter is to be
26+
* applied.
27+
*/
28+
std::string getName() const;
29+
30+
/*
31+
* Constraint type i.e. LT, LTE, EQ, NEQ etc.
32+
*/
33+
std::string getType() const;
34+
35+
int getValue() const;
36+
37+
private:
38+
std::string name_;
39+
std::string type_;
40+
int value_;
41+
};
42+
43+
} // namespace pc_translator
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#include "fbpcs/pc_translator/input_processing/PCInstructionSet.h"
9+
10+
#include <folly/json.h>
11+
#include <cstdint>
12+
#include <memory>
13+
#include <string>
14+
#include <vector>
15+
16+
namespace pc_translator {
17+
18+
const std::vector<std::string>& PCInstructionSet::getGroupByIds() const {
19+
return groupByIds;
20+
}
21+
22+
const std::vector<FilterConstraint>& PCInstructionSet::getFilterConstraints()
23+
const {
24+
return filterConstraints;
25+
}
26+
27+
PCInstructionSet PCInstructionSet::fromDynamic(const folly::dynamic& obj) {
28+
PCInstructionSet pcInstructionSet;
29+
auto aggregationConfig = obj["aggregated_metrics"];
30+
auto groupByFields = aggregationConfig["group_by"];
31+
32+
for (auto groupByField : groupByFields) {
33+
pcInstructionSet.groupByIds.push_back(groupByField.asString());
34+
}
35+
36+
auto filterConstraintsFields = aggregationConfig["filter"];
37+
38+
for (auto& [key, constraints] : filterConstraintsFields.items()) {
39+
std::string name = key.asString();
40+
for (auto constraint : constraints) {
41+
auto constraintType = constraint["constraint_type"].asString();
42+
auto constraintValue = constraint["value"].asInt();
43+
FilterConstraint filterConstraint(name, constraintType, constraintValue);
44+
pcInstructionSet.filterConstraints.push_back(filterConstraint);
45+
}
46+
}
47+
48+
return pcInstructionSet;
49+
}
50+
51+
} // namespace pc_translator
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#pragma once
9+
10+
#include <folly/json.h>
11+
#include <cstdint>
12+
#include <memory>
13+
#include <string>
14+
#include <vector>
15+
#include "fbpcs/pc_translator/input_processing/FilterConstraint.h"
16+
17+
namespace pc_translator {
18+
19+
/*
20+
* Class to store PC Instruction set. This class contains a list of group Ids as
21+
* well as list of filter constraints.
22+
*/
23+
class PCInstructionSet {
24+
public:
25+
/*
26+
* Method to all group Ids from the PC instruction set.
27+
*/
28+
const std::vector<std::string>& getGroupByIds() const;
29+
30+
/*
31+
* Method to get all filter constraints from PC instruction set.
32+
*/
33+
const std::vector<FilterConstraint>& getFilterConstraints() const;
34+
35+
/*
36+
* Method to get parse and create PCInstructionSet instance.
37+
*/
38+
static PCInstructionSet fromDynamic(const folly::dynamic& obj);
39+
40+
private:
41+
std::vector<std::string> groupByIds;
42+
std::vector<FilterConstraint> filterConstraints;
43+
44+
void parseJson(const std::string& json);
45+
};
46+
47+
} // namespace pc_translator
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#include <folly/json.h>
9+
#include <filesystem>
10+
#include <string>
11+
12+
#include <fbpcf/io/api/FileIOWrappers.h>
13+
#include <gtest/gtest.h>
14+
#include "../../../emp_games/common/TestUtil.h"
15+
#include "fbpcs/pc_translator/input_processing/PCInstructionSet.h"
16+
#include "folly/Random.h"
17+
18+
namespace pc_translator {
19+
class TestPCInstructionSet : public ::testing::Test {
20+
public:
21+
protected:
22+
std::string testInstructionSetPath_;
23+
24+
void SetUp() override {
25+
std::string baseDir =
26+
private_measurement::test_util::getBaseDirFromPath(__FILE__);
27+
testInstructionSetPath_ = baseDir + "test_instruction_set.json";
28+
}
29+
};
30+
31+
TEST_F(TestPCInstructionSet, TestStandardWorkflowTest) {
32+
auto pcInstructionSet = std::make_shared<PCInstructionSet>(
33+
PCInstructionSet::fromDynamic(folly::parseJson(
34+
fbpcf::io::FileIOWrappers::readFile(testInstructionSetPath_))));
35+
auto groupByIds = pcInstructionSet->getGroupByIds();
36+
auto filterConstraints = pcInstructionSet->getFilterConstraints();
37+
EXPECT_EQ(groupByIds.size(), 2);
38+
EXPECT_EQ(filterConstraints.size(), 4);
39+
EXPECT_EQ(filterConstraints[0].getName(), "gender");
40+
EXPECT_EQ(filterConstraints[0].getType(), "EQ");
41+
EXPECT_EQ(filterConstraints[0].getValue(), 0);
42+
}
43+
44+
} // namespace pc_translator
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"publisher_input": {
3+
"num_impressions": "int",
4+
"num_clicks": "int",
5+
"total_spend": "int",
6+
"opportunity_timstamp": "int",
7+
"test_flag": "int",
8+
"age": "int",
9+
"gender": "Optional[int]",
10+
"breakdown_id": "Optional[int]"
11+
},
12+
"partner_input": {
13+
"value": "int",
14+
"event_timestamp": "int",
15+
"partner_cohort_id": "Optional[int]"
16+
},
17+
"aggregated_metrics": {
18+
"filter": {
19+
"age": [
20+
{
21+
"constraint_type": "GTE",
22+
"value": "25"
23+
},
24+
{
25+
"constraint_type": "LTE",
26+
"value": "40"
27+
}
28+
],
29+
"gender": [
30+
{
31+
"constraint_type": "EQ",
32+
"value": "0"
33+
},
34+
{
35+
"constraint_type": "EQ",
36+
"value": "1"
37+
}
38+
]
39+
},
40+
"group_by": [
41+
"age",
42+
"gender"
43+
]
44+
}
45+
}

0 commit comments

Comments
 (0)