MCPcopy Create free account
hub / github.com/ModelTC/LightX2V / get_source_tensor

Function get_source_tensor

lightx2v/common/ops/utils.py:35–73  ·  view source on GitHub ↗

Get the source tensor from either weight dictionary or lazy loading safetensors file. Args: source_name: Name of the target tensor to get weight_dict: Preloaded weight dictionary lazy_load: Whether to enable lazy loading mode lazy_load_file: File or directory pat

(source_name, weight_dict, lazy_load, lazy_load_file, use_infer_dtype, scale_force_fp32, bias_force_fp32)

Source from the content-addressed store, hash-verified

33 return re.sub(r"\.\d+", lambda m: f".{adapter_block_index}", name, count=1)
34 else:
35 return re.sub(r"\.\d+", lambda m: f".{block_index}", name, count=1)
36
37
38def get_source_tensor(source_name, weight_dict, lazy_load, lazy_load_file, use_infer_dtype, scale_force_fp32, bias_force_fp32, *, dtype=None):
39 """Get the source tensor from either weight dictionary or lazy loading safetensors file.
40
41 Args:
42 source_name: Name of the target tensor to get
43 weight_dict: Preloaded weight dictionary
44 lazy_load: Whether to enable lazy loading mode
45 lazy_load_file: File or directory path for lazy loading
46 use_infer_dtype: Whether to convert tensor to inference dtype
47 scale_force_fp32: Whether to force weight_scale tensors to float32
48 bias_force_fp32: Whether to force bias tensors to float32
49 dtype: Explicit output dtype, overriding the global inference dtype when supplied
50
51 Returns:
52 The target tensor retrieved from the source with appropriate dtype conversion applied
53 """
54 if lazy_load:
55 if Path(lazy_load_file).is_file():
56 lazy_load_file_path = lazy_load_file
57 else:
58 lazy_load_file_path = os.path.join(
59 lazy_load_file,
60 f"block_{source_name.split('.')[1]}.safetensors",
61 )
62 with safe_open(lazy_load_file_path, framework="pt", device="cpu") as lazy_load_file:
63 tensor = lazy_load_file.get_tensor(source_name)
64 else:
65 tensor = weight_dict[source_name]
66
67 if dtype is not None:
68 return tensor.to(dtype)
69 if use_infer_dtype:
70 return tensor.to(GET_DTYPE())
71 if (scale_force_fp32 and "weight_scale" in source_name) or (bias_force_fp32 and "bias" in source_name):
72 return tensor.to(torch.float32)
73 return tensor
74
75
76def create_pin_tensor(tensor, transpose=False, dtype=None):

Callers 2

create_cuda_buffersFunction · 0.85
create_cpu_buffersFunction · 0.85

Calls 2

GET_DTYPEFunction · 0.50
toMethod · 0.45

Tested by

no test coverage detected