Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/CUDApp/blob/main/tests/amd-exec/fp8block_fast_notebook/simulated_device.py # CUDApp CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / tests / amd-exec / fp8block_fast_notebook / simulated_device.py` 163 lines · 8382 bytes Raw Blame History 1 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 2 # TEST INJECTION ONLY. No hardware, driver, GPU, or AMD library is exercised. 3 import signal, resource, types 4 resource.setrlimit(resource.RLIMIT_CORE,(0,0)) 5 SIMULATED=True 6 _original_run=subprocess.run 7 8 def _simulation_run(argv,*args,**kwargs): 9 if isinstance(argv,str) and argv.startswith('rocminfo '): 10 return subprocess.CompletedProcess(argv,0,'SIMULATED host; no device query\n','') 11 return _original_run(argv,*args,**kwargs) 12 subprocess.run=_simulation_run 13 14 15 def _simulation_result(case,mode): 16 m,k=case['m'],case['k']; tab=fp8_table(); x=case['x'].astype(np.float64) 17 flat=(case['scales'].astype(np.uint32)<<16).view(np.float32).ravel().astype(np.float64) 18 nb=(k+127)//128; mb=(m+127)//128; out=np.zeros(m,dtype=np.float64) 19 stop=k//128*128 if mode=='no-tail-block' else k 20 for row in range(m): 21 columns=np.arange(stop)//128 22 indices=(row*nb+columns if mode=='per-row-scale' else columns*mb+row//128 if mode=='transposed-scale' else (row//128)*nb+columns) 23 scales=np.zeros(stop,dtype=np.float64) 24 valid=indices=len(data): return np.zeros(self.shape,dtype=self.storage.dtype) 108 assert self.start+self.count<=len(data) 109 return data[self.start:self.start+self.count].reshape(self.shape) 110 def __getitem__(self,item): 111 assert isinstance(item,slice) and item.step is None 112 start=0 if item.start is None else item.start; end=self.count if item.stop is None else item.stop 113 return Tensor(self.storage,start=self.start+start,count=end-start) 114 def reshape(self,*shape): 115 assert math.prod(shape)==self.count 116 return Tensor(self.storage,shape,self.start,self.count) 117 def copy_(self,other): 118 assert self.start==0 and self.count==other.count and self.storage.dtype==other.storage.dtype 119 self.storage.data=other.array().reshape(-1) 120 return self 121 def view(self,dtype): 122 assert dtype=='bf16' and self.storage.dtype==np.uint16 123 return self 124 def to(self,dtype): 125 assert dtype=='bf16' 126 value=self.array().astype(np.float32); bits=value.view(np.uint32) 127 data=((bits+0x7fff+((bits>>16)&1))>>16).astype(np.uint16) 128 data[np.isnan(value)]=0x7fc0 129 return Tensor(Storage(data.size,np.uint16,data.ravel()),data.shape) 130 def float(self): 131 assert self.storage.dtype==np.uint16 132 data=(self.array().astype(np.uint32)<<16).view(np.float32) 133 return Tensor(Storage(data.size,np.float32,data.ravel()),data.shape) 134 def cpu(self): return self 135 def numpy(self): return self.array() 136 137 138 class Torch: 139 bfloat16='bf16'; __version__='SIMULATED'; version=types.SimpleNamespace(hip='SIMULATED') 140 cuda=types.SimpleNamespace(is_available=lambda:True,synchronize=lambda:None,get_device_name=lambda:'SIMULATED_gfx942') 141 def __init__(self): self.cache={} 142 def zeros(self,n,dtype,device): 143 assert dtype=='bf16' and device=='cuda' and n>=SLACK//2 144 return Tensor(Storage(n,np.uint16)) 145 def from_numpy(self,data): return Tensor(Storage(data.size,data.dtype,data.ravel()),data.shape) 146 def count_nonzero(self,t): return types.SimpleNamespace(item=lambda:int(np.count_nonzero(t.array()))) 147 def matmul(self,a,x,out): 148 # All matrix copies alias immutable simulation storage, not real device allocations. 149 base=a.storage.data 150 while getattr(base,'base',None) is not None: base=base.base 151 key=(id(base),id(x.storage.data),a.shape) 152 if key not in self.cache: 153 w=a.array(); xf=x.float().array().astype(np.float64); result=np.zeros(a.shape[0],dtype=np.float32) 154 for r in range(0,a.shape[0],128): 155 wf=(w[r:r+128].astype(np.uint32)<<16).view(np.float32).astype(np.float64) 156 result[r:r+128]=wf@xf 157 bits=result.view(np.uint32) 158 words=((bits+0x7fff+((bits>>16)&1))>>16).astype(np.uint16); words[np.isnan(result)]=0x7fc0 159 self.cache[key]=words 160 out.storage.data=self.cache[key] 161 return out 162 163 sys.modules['torch']=Torch()