Skip to content

Commit 1f0aeda

Browse files
committed
[tmva][sofie] Replace template overload of RModel::AddInitializedTensor
This makes the overload that takes memory by raw pointer more consistent with the other overload that takes a `std::shared_ptr<void>`. This avoids also the need for some template instantiations on the SOFIE Pythonizations side, saving some memory cost.
1 parent 97201e1 commit 1f0aeda

4 files changed

Lines changed: 16 additions & 15 deletions

File tree

bindings/pyroot/pythonizations/python/ROOT/_pythonization/_tmva/_gnn.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ def add_weights(gin, weights, function_target):
147147
shape_as_list = i.shape.as_list()
148148
for j in shape_as_list:
149149
shape.push_back(j)
150-
model_block.GetFunctionBlock().AddInitializedTensor['float'](i.name, shape, i.numpy())
150+
model_block.GetFunctionBlock().AddInitializedTensor(i.name, SOFIE.ETensorType.FLOAT, shape, i.numpy())
151+
151152

152153
def add_aggregate_function(gin, reducer, relation):
153154
"""

bindings/pyroot/pythonizations/python/ROOT/_pythonization/_tmva/_sofie/_parser/_keras/parser.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,7 @@ def move_operator(op):
136136
# in c++ that does the conversion from a regular pointer to unique one in c++
137137
# print('adding initialized tensor..',LayerName, TargetShape)
138138
shape_tensor_name = LayerName + "_shape"
139-
shape_data = TargetShape.data
140-
print(TargetShape, shape_data)
141-
print(len(TargetShape))
142-
rmodel.AddInitializedTensor["int64_t"](shape_tensor_name, [len(TargetShape)], shape_data)
139+
rmodel.AddInitializedTensor(shape_tensor_name, SOFIE.ETensorType.UINT64, [len(TargetShape)], TargetShape)
143140

144141
# These layers only have one operator - excluding the recurrent layers, in which the activation function(s)
145142
# are included in the recurrent operator
@@ -512,7 +509,7 @@ def Parse(filename, batch_size=1): # If a model does not have a defined batch s
512509

513510
else:
514511
fData = fWeightArray.flatten()
515-
rmodel.AddInitializedTensor["float"](fWeightName, fWeightTensorShape, fData)
512+
rmodel.AddInitializedTensor(fWeightName, SOFIE.ETensorType.FLOAT, fWeightTensorShape, fData)
516513
else:
517514
raise TypeError("Type error: TMVA SOFIE does not yet support data layer type: " + fWeightDType)
518515

tmva/sofie/inc/TMVA/RModel.hxx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ public:
7676
void AddOperator(std::unique_ptr<ROperator> op, int order_execution = -1);
7777
void AddInitializedTensor(std::string tensor_name, ETensorType type, std::vector<std::size_t> shape,
7878
std::shared_ptr<void> data);
79+
void AddInitializedTensor(const std::string &tensor_name, ETensorType tensor_type,
80+
const std::vector<std::size_t> &shape, void *raw_data);
7981
void AddConstantTensor(std::string tensor_name, ETensorType type, std::vector<std::size_t> shape,
8082
std::shared_ptr<void> data);
8183

@@ -99,15 +101,6 @@ public:
99101
AddConstantTensor(name, GetTemplatedType<T>(T()), shape, data_ptr);
100102
}
101103

102-
template <typename T>
103-
void AddInitializedTensor(const std::string & tensor_name, const std::vector<std::size_t> & shape, T *raw_data)
104-
{
105-
size_t size = ConvertShapeToLength(shape);
106-
std::shared_ptr<void> data(malloc(size * sizeof(T)), free);
107-
std::memcpy(data.get(), raw_data, size * sizeof(T));
108-
AddInitializedTensor(tensor_name, GetTemplatedType(T()), shape, data);
109-
}
110-
111104
void AddShapeTensor(const std::string & name, const std::vector<Dim> & shapeValues, bool scalar = false);
112105

113106
void AddExtraCodeForDimShapes(const std::string & code) { fExtraCodeForDimShapes += code; }

tmva/sofie/src/RModel.cxx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,16 @@ void RModel::AddInitializedTensor(std::string tensor_name, ETensorType type, std
229229
fInitializedTensors[tensor_name] = new_tensor;
230230
}
231231

232+
void RModel::AddInitializedTensor(const std::string &tensor_name, ETensorType tensor_type,
233+
const std::vector<std::size_t> &shape, void *raw_data)
234+
{
235+
size_t size = ConvertShapeToLength(shape);
236+
auto itemsize = GetTypeSize(tensor_type);
237+
std::shared_ptr<void> data(malloc(size * itemsize), free);
238+
std::memcpy(data.get(), raw_data, size * itemsize);
239+
AddInitializedTensor(tensor_name, tensor_type, shape, data);
240+
}
241+
232242
void RModel::AddConstantTensor(std::string tensor_name, ETensorType type, std::vector<std::size_t> shape, std::shared_ptr<void> data) {
233243
tensor_name = UTILITY::Clean_name(tensor_name);
234244
//NB: own data

0 commit comments

Comments
 (0)