Stream¶
- class nvtripy.Stream(priority: int = 0)[source]¶
Bases:
object
Represents a CUDA stream that can be used to manage concurrent operations.
Note
Streams can only be used with compiled functions.
This class is a wrapper around the underlying stream object, allowing management of CUDA streams.
- Parameters:
priority (int) – Assign priority for the new stream. Lower number signifies higher priority.
Example: Creating New Streams
1stream_a = tp.Stream() 2stream_b = tp.Stream()
Local Variables¶>>> stream_a <Stream(id=133553298685472)> >>> stream_b <Stream(id=133553298682352)>
Example: Using Streams With Compiled Functions
1func = tp.compile(tp.relu, args=[tp.InputInfo((2, 2), dtype=tp.float32)]) 2 3# Run the compiled linear function on a custom stream: 4stream = tp.Stream() 5func.stream = stream 6 7input = tp.ones((2, 2), dtype=tp.float32) 8output = func(input)
Local Variables¶>>> stream <Stream(id=133553343611424)> >>> input tensor( [[1, 1], [1, 1]], dtype=float32, loc=gpu:0, shape=(2, 2)) >>> output tensor( [[1, 1], [1, 1]], dtype=float32, loc=gpu:0, shape=(2, 2))
- synchronize() None [source]¶
Synchronize the stream, blocking until all operations in this stream are complete.
Example: Using Synchronize For Benchmarking
1import time 2 3func = tp.compile(tp.relu, args=[tp.InputInfo((2, 2), dtype=tp.float32)]) 4 5input = tp.ones((2, 2), dtype=tp.float32) 6 7func.stream = tp.Stream() 8 9num_iters = 10 10start_time = time.perf_counter() 11for _ in range(num_iters): 12 _ = func(input) 13func.stream.synchronize() 14end_time = time.perf_counter() 15 16time = (end_time - start_time) / num_iters 17print(f"Execution took {time * 1000} ms")
Output¶Execution took 2.704306799569167 ms
- Return type:
None
- nvtripy.default_stream(device: device = device(kind='gpu', index=0)) Stream [source]¶
Provides access to the default Tripy CUDA stream for a given device. There is only one default stream instance per device.
- Parameters:
device (device) – The device for which to get the default stream.
- Returns:
The default stream for the specified device.
- Raises:
TripyException – If the device is not of type ‘gpu’ or if the device index is not 0.
- Return type:
Note
Calling
default_stream()
with the same device always returns the sameStream
instance for that device.Example: Retrieving The Default Stream
1# Get the default stream for the current device. 2default = tp.default_stream()
Local Variables¶>>> default <Stream(id=133553300060288)>