Skip to content

Commit b59e6fe

Browse files
Merge branch 'master' into r0.14
2 parents 0ef8393 + ae7191c commit b59e6fe

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

src/ngraph_builder.cc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2232,6 +2232,34 @@ static Status TranslateL2LossOp(
22322232
return Status::OK();
22332233
}
22342234

2235+
static Status TranslateLogSoftmaxOp(
2236+
const Node* op, const std::vector<const Tensor*>& static_input_map,
2237+
Builder::OpMap& ng_op_map) {
2238+
shared_ptr<ng::Node> ng_inp;
2239+
TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_inp));
2240+
auto inp_shape = ng_inp->get_shape();
2241+
int rank = inp_shape.size();
2242+
auto ng_axis = ng::AxisSet{rank - 1};
2243+
// Batch i, class j
2244+
// logsoftmax[i, j] = logits[i, j] - log(sum(exp(logits[i])))
2245+
// Actually implementing: logsoftmax[i, j] = logits[i, j] - max(logits[i]) -
2246+
// log(sum(exp(logits[i] - max(logits[i]))))
2247+
auto ng_max = ConstructNgNode<ng::op::Broadcast>(
2248+
op->name(), ConstructNgNode<ng::op::Max>(op->name(), ng_inp, ng_axis),
2249+
inp_shape, ng_axis);
2250+
auto ng_inp_minus_max =
2251+
ConstructNgNode<ng::op::Subtract>(op->name(), ng_inp, ng_max);
2252+
auto ng_exp = ConstructNgNode<ng::op::Exp>(op->name(), ng_inp_minus_max);
2253+
auto ng_log_sum = ConstructNgNode<ng::op::Log>(
2254+
op->name(), ConstructNgNode<ng::op::Sum>(op->name(), ng_exp, ng_axis));
2255+
auto ng_broadcast = ConstructNgNode<ng::op::Broadcast>(
2256+
op->name(), ng_log_sum, ng_inp->get_shape(), ng_axis);
2257+
auto ng_output = ConstructNgNode<ng::op::Subtract>(
2258+
op->name(), ng_inp_minus_max, ng_broadcast);
2259+
SaveNgOp(ng_op_map, op->name(), ng_output);
2260+
return Status::OK();
2261+
}
2262+
22352263
static Status TranslateMatMulOp(
22362264
const Node* op, const std::vector<const Tensor*>& static_input_map,
22372265
Builder::OpMap& ng_op_map) {
@@ -4525,6 +4553,7 @@ const static std::map<
45254553
{"HorovodAllreduce", TranslateAllreduceOp},
45264554
{"Identity", TranslateIdentityOp},
45274555
{"L2Loss", TranslateL2LossOp},
4556+
{"LogSoftmax", TranslateLogSoftmaxOp},
45284557
{"Less", TranslateBinaryOp<ngraph::op::Less>},
45294558
{"LessEqual", TranslateBinaryOp<ngraph::op::LessEq>},
45304559
{"Log", TranslateUnaryOp<ngraph::op::Log>},

src/ngraph_mark_for_clustering.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ Status MarkForClustering(Graph* graph,
281281
#endif
282282
confirmation_function_map["Identity"] = SimpleConfirmationFunction();
283283
confirmation_function_map["L2Loss"] = SimpleConfirmationFunction();
284+
confirmation_function_map["LogSoftmax"] = SimpleConfirmationFunction();
284285
confirmation_function_map["Less"] = SimpleConfirmationFunction();
285286
confirmation_function_map["LessEqual"] = SimpleConfirmationFunction();
286287
confirmation_function_map["Log"] = SimpleConfirmationFunction();
@@ -449,6 +450,7 @@ Status MarkForClustering(Graph* graph,
449450
#endif
450451
type_constraint_map["Identity"]["T"] = NGraphDTypes();
451452
type_constraint_map["L2Loss"]["T"] = NGraphNumericDTypes();
453+
type_constraint_map["LogSoftmax"]["T"] = NGraphRealDTypes();
452454
type_constraint_map["Less"]["T"] = NGraphDTypes();
453455
type_constraint_map["LessEqual"]["T"] = NGraphDTypes();
454456
type_constraint_map["Log"]["T"] = NGraphNumericDTypes();

test/python/tensorflow/python_tests_list_gpu.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,10 +407,10 @@ slice_op_test.SliceTest.testSliceOfSlice
407407
#softmax_op_test.SoftmaxTest.test1DTensorAsInputNoReshape
408408
#softmax_op_test.SoftmaxTest.test3DTensorAsInput
409409
#softmax_op_test.SoftmaxTest.test3DTensorAsInputNoReshape
410-
softmax_op_test.SoftmaxTest.testAlongFirstDimension
411-
softmax_op_test.SoftmaxTest.testAlongSecondDimension
410+
#softmax_op_test.SoftmaxTest.testAlongFirstDimension
411+
#softmax_op_test.SoftmaxTest.testAlongSecondDimension
412412
softmax_op_test.SoftmaxTest.testDimTooLarge
413-
softmax_op_test.SoftmaxTest.testDouble
413+
#softmax_op_test.SoftmaxTest.testDouble
414414
softmax_op_test.SoftmaxTest.testEmptyInput
415415
softmax_op_test.SoftmaxTest.testFloat
416416
#softmax_op_test.SoftmaxTest.testFloatGPU

test/test_nn_ops.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,30 @@ TEST(NNOps, L2Loss) {
11161116
}
11171117
}
11181118

1119+
// Test Op :"LogSoftmax"
1120+
TEST(NNOps, LogSoftmax) {
1121+
std::vector<std::vector<int64>> input_sizes = {
1122+
{3}, {3, 2}, {5, 6}, {3, 4, 5}, {2, 3, 4, 5}};
1123+
1124+
vector<int> static_input_indexes = {};
1125+
1126+
for (auto const& input_size : input_sizes) {
1127+
Scope root = Scope::NewRootScope();
1128+
1129+
Tensor input_data(DT_FLOAT, TensorShape(input_size));
1130+
AssignInputValuesRandom<float>(input_data, -2, 2);
1131+
1132+
auto R = ops::LogSoftmax(root, input_data);
1133+
vector<DataType> output_datatypes = {DT_FLOAT};
1134+
std::vector<Output> sess_run_fetchoutputs = {R};
1135+
1136+
OpExecuter opexecuter(root, "LogSoftmax", static_input_indexes,
1137+
output_datatypes, sess_run_fetchoutputs);
1138+
1139+
opexecuter.RunTest();
1140+
}
1141+
}
1142+
11191143
// Test Op :"MaxPool3D"
11201144
TEST(NNOps, MaxPool3DNDHWCSame) {
11211145
std::vector<std::vector<int64>> input_sizes;

0 commit comments

Comments
 (0)