MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / ArgOpShape

Function ArgOpShape

tensorflow/core/ops/math_ops.cc:1138–1191  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1136namespace {
1137
1138Status ArgOpShape(shape_inference::InferenceContext* c) {
1139 ShapeHandle dimension_shape;
1140 TF_RETURN_IF_ERROR(c->WithRank(c->input(1), 0, &dimension_shape));
1141
1142 ShapeHandle input_shape = c->input(0);
1143 if (!c->RankKnown(input_shape)) {
1144 return shape_inference::UnknownShape(c);
1145 }
1146
1147 const int32 input_rank = c->Rank(input_shape);
1148 if (input_rank <= 1) {
1149 // Reducing a scalar/vector must return a scalar.
1150 return shape_inference::ScalarShape(c);
1151 }
1152
1153 const Tensor* dim_t = c->input_tensor(1);
1154 if (dim_t == nullptr) {
1155 // We don't know the value of the dimension, but we
1156 // know the rank of the input, so return the correct
1157 // rank with unknown dimensions.
1158 std::vector<DimensionHandle> dims(input_rank - 1);
1159 for (int i = 0; i < dims.size(); ++i) {
1160 dims[i] = c->UnknownDim();
1161 }
1162
1163 c->set_output(0, c->MakeShape(dims));
1164 return Status::OK();
1165 }
1166
1167 int64 dimension_val;
1168 if (dim_t->dtype() == DT_INT32) {
1169 dimension_val = dim_t->scalar<int32>()();
1170 } else {
1171 dimension_val = dim_t->scalar<int64>()();
1172 }
1173
1174 int64 axis = dimension_val < 0 ? dimension_val + input_rank : dimension_val;
1175 if (axis < 0 || axis >= input_rank) {
1176 return errors::InvalidArgument(
1177 "Dimension (", dimension_val, ") must be in the range [", -input_rank,
1178 ", ", input_rank, "), where ", input_rank,
1179 " is the number of dimensions in the input.");
1180 }
1181
1182 // Return the input shape without the dimension being reduced.
1183 std::vector<DimensionHandle> dims;
1184 for (int i = 0; i < input_rank; ++i) {
1185 if (axis != i) {
1186 dims.emplace_back(c->Dim(input_shape, i));
1187 }
1188 }
1189 c->set_output(0, c->MakeShape(dims));
1190 return Status::OK();
1191}
1192
1193} // namespace
1194

Callers

nothing calls this directly

Calls 15

InvalidArgumentFunction · 0.85
RankKnownMethod · 0.80
UnknownDimMethod · 0.80
UnknownShapeFunction · 0.50
ScalarShapeFunction · 0.50
WithRankMethod · 0.45
inputMethod · 0.45
RankMethod · 0.45
input_tensorMethod · 0.45
sizeMethod · 0.45
set_outputMethod · 0.45
MakeShapeMethod · 0.45

Tested by

no test coverage detected