| 310 | } // namespace |
| 311 | |
| 312 | ValueRefList ScalarTransformation::apply_get_attr( |
| 313 | const GetAttr& get_attr, Span<ValueRef> inputs) { |
| 314 | auto&& input = inputs.item(); |
| 315 | bool is_scalar = input.is(m_value_type); |
| 316 | if (!is_scalar) { |
| 317 | return imperative::apply(get_attr, input); |
| 318 | } |
| 319 | auto unwrapped_input = input.cast(m_value_type).value(); |
| 320 | if (get_attr.attr() == GetAttr::Shape) { |
| 321 | if (!m_empty_shape) { |
| 322 | m_empty_shape = ShapeValue::make(); |
| 323 | } |
| 324 | return {m_empty_shape}; |
| 325 | } else { |
| 326 | auto outputs = imperative::apply(get_attr, unwrapped_input); |
| 327 | auto& output = outputs[0]; |
| 328 | switch (get_attr.attr()) { |
| 329 | case GetAttr::Value: { |
| 330 | auto& hv = output.cast<HostValue>(); |
| 331 | mgb_assert( |
| 332 | hv.shape() == ValueShape({1}), |
| 333 | "underlying value should has shape {1}, got %s", |
| 334 | hv.shape().to_string().c_str()); |
| 335 | output = HostValue::make(hv.dtype(), ValueShape(), hv.storage()); |
| 336 | break; |
| 337 | } |
| 338 | case GetAttr::Data: { |
| 339 | auto& dv = output.cast<DeviceValue>(); |
| 340 | mgb_assert( |
| 341 | dv.shape() == ValueShape({1}), |
| 342 | "underlying value should has shape {1}, got %s", |
| 343 | dv.shape().to_string().c_str()); |
| 344 | output = DeviceValue::make(dv.dtype(), ValueShape(), dv.storage()); |
| 345 | break; |
| 346 | } |
| 347 | default: |
| 348 | break; |
| 349 | } |
| 350 | return outputs; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | ValueRefList ScalarTransformation::apply_transformation( |
| 355 | const Operator& op, Span<ValueRef> inputs) { |
nothing calls this directly
no test coverage detected