|
21 | 21 | PRECISION_DICT, |
22 | 22 | RESERVED_PRECISION_DICT, |
23 | 23 | NativeOP, |
| 24 | + get_xp_precision, |
24 | 25 | ) |
25 | 26 | from deepmd.dpmodel.model.base_model import ( |
26 | 27 | BaseModel, |
@@ -103,7 +104,8 @@ def model_call_from_call_lower( |
103 | 104 | bb.reshape(nframes, 3, 3), |
104 | 105 | ) |
105 | 106 | else: |
106 | | - coord_normalized = cc.copy() |
| 107 | + xp = array_api_compat.array_namespace(cc) |
| 108 | + coord_normalized = xp.reshape(cc, (nframes, nloc, 3)) |
107 | 109 | extended_coord, extended_atype, mapping = extend_coord_with_ghosts( |
108 | 110 | coord_normalized, atype, bb, rcut |
109 | 111 | ) |
@@ -371,53 +373,73 @@ def input_type_cast( |
371 | 373 | box: Array | None = None, |
372 | 374 | fparam: Array | None = None, |
373 | 375 | aparam: Array | None = None, |
374 | | - ) -> tuple[Array, Array, np.ndarray | None, np.ndarray | None, str]: |
| 376 | + ) -> tuple[Array, Array | None, Array | None, Array | None, Any]: |
375 | 377 | """Cast the input data to global float type.""" |
376 | | - input_prec = RESERVED_PRECISION_DICT[self.precision_dict[coord.dtype.name]] |
| 378 | + xp = array_api_compat.array_namespace(coord) |
| 379 | + input_dtype = coord.dtype |
| 380 | + global_dtype = get_xp_precision( |
| 381 | + xp, RESERVED_PRECISION_DICT[self.global_np_float_precision] |
| 382 | + ) |
377 | 383 | ### |
378 | 384 | ### type checking would not pass jit, convert to coord prec anyway |
379 | 385 | ### |
380 | | - _lst: list[np.ndarray | None] = [ |
381 | | - vv.astype(coord.dtype) if vv is not None else None |
| 386 | + _lst: list[Array | None] = [ |
| 387 | + xp.astype(vv, input_dtype) if vv is not None else None |
382 | 388 | for vv in [box, fparam, aparam] |
383 | 389 | ] |
384 | 390 | box, fparam, aparam = _lst |
385 | | - if input_prec == RESERVED_PRECISION_DICT[self.global_np_float_precision]: |
386 | | - return coord, box, fparam, aparam, input_prec |
| 391 | + if input_dtype == global_dtype: |
| 392 | + return coord, box, fparam, aparam, input_dtype |
387 | 393 | else: |
388 | | - pp = self.global_np_float_precision |
389 | 394 | return ( |
390 | | - coord.astype(pp), |
391 | | - box.astype(pp) if box is not None else None, |
392 | | - fparam.astype(pp) if fparam is not None else None, |
393 | | - aparam.astype(pp) if aparam is not None else None, |
394 | | - input_prec, |
| 395 | + xp.astype(coord, global_dtype), |
| 396 | + xp.astype(box, global_dtype) if box is not None else None, |
| 397 | + xp.astype(fparam, global_dtype) if fparam is not None else None, |
| 398 | + xp.astype(aparam, global_dtype) if aparam is not None else None, |
| 399 | + input_dtype, |
395 | 400 | ) |
396 | 401 |
|
397 | 402 | def output_type_cast( |
398 | 403 | self, |
399 | 404 | model_ret: dict[str, Array], |
400 | | - input_prec: str, |
| 405 | + input_prec: Any, |
401 | 406 | ) -> dict[str, Array]: |
402 | | - """Convert the model output to the input prec.""" |
403 | | - do_cast = ( |
404 | | - input_prec != RESERVED_PRECISION_DICT[self.global_np_float_precision] |
| 407 | + """Convert the model output to the input prec. |
| 408 | +
|
| 409 | + Parameters |
| 410 | + ---------- |
| 411 | + model_ret |
| 412 | + The model output. |
| 413 | + input_prec |
| 414 | + The input dtype returned by ``input_type_cast``. |
| 415 | + """ |
| 416 | + model_ret_not_none = [vv for vv in model_ret.values() if vv is not None] |
| 417 | + if not model_ret_not_none: |
| 418 | + return model_ret |
| 419 | + xp = array_api_compat.array_namespace(model_ret_not_none[0]) |
| 420 | + global_dtype = get_xp_precision( |
| 421 | + xp, RESERVED_PRECISION_DICT[self.global_np_float_precision] |
| 422 | + ) |
| 423 | + ener_dtype = get_xp_precision( |
| 424 | + xp, RESERVED_PRECISION_DICT[self.global_ener_float_precision] |
405 | 425 | ) |
406 | | - pp = self.precision_dict[input_prec] |
| 426 | + do_cast = input_prec != global_dtype |
407 | 427 | odef = self.model_output_def() |
408 | 428 | for kk in odef.keys(): |
409 | 429 | if kk not in model_ret.keys(): |
410 | 430 | # do not return energy_derv_c if not do_atomic_virial |
411 | 431 | continue |
412 | 432 | if check_operation_applied(odef[kk], OutputVariableOperation.REDU): |
413 | 433 | model_ret[kk] = ( |
414 | | - model_ret[kk].astype(self.global_ener_float_precision) |
| 434 | + xp.astype(model_ret[kk], ener_dtype) |
415 | 435 | if model_ret[kk] is not None |
416 | 436 | else None |
417 | 437 | ) |
418 | 438 | elif do_cast: |
419 | 439 | model_ret[kk] = ( |
420 | | - model_ret[kk].astype(pp) if model_ret[kk] is not None else None |
| 440 | + xp.astype(model_ret[kk], input_prec) |
| 441 | + if model_ret[kk] is not None |
| 442 | + else None |
421 | 443 | ) |
422 | 444 | return model_ret |
423 | 445 |
|
|
0 commit comments