tqchen commented on code in PR #5: URL: https://github.com/apache/tvm-ffi/pull/5#discussion_r2347616567
########## python/tvm_ffi/stream.py: ########## @@ -0,0 +1,102 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# pylint: disable=invalid-name +"""Stream context.""" +from ctypes import c_void_p +from typing import Any, Optional, Union + +from . import core +from ._tensor import device + +try: + import torch + + class TorchStreamContext: + def __init__(self, context: Optional[Any]): + self.torch_context = context + + def __enter__(self): + if self.torch_context: + self.torch_context.__enter__() + current_stream = torch.cuda.current_stream() + self.ffi_context = core.StreamContext( + device(str(current_stream.device)), current_stream.cuda_stream + ) + self.ffi_context.__enter__() + + def __exit__(self, *args): + if self.torch_context: + self.torch_context.__exit__(*args) + self.ffi_context.__exit__(*args) + + def use_torch_stream(context: Optional[Any] = None): + """ + Create a ffi stream context with given torch stream, + cuda graph or current stream if `None` provided. + + Parameters + ---------- + context : Optional[Any] + The wrapped torch stream or cuda graph. + + Returns + ------- + context : tvm_ffi.TorchStreamContext + The ffi stream context wrapping torch stream context. + + Examples + -------- + .. code-block:: python + s = torch.cuda.Stream() Review Comment: need indent one level and a blank line between code block and code ########## python/tvm_ffi/cython/base.pxi: ########## @@ -245,6 +246,43 @@ cdef extern from "tvm/ffi/extra/c_env_api.h": TVMFFIStreamHandle stream, TVMFFIStreamHandle* opt_out_original_stream) nogil +cdef _env_set_current_stream(int device_type, int device_id, uint64_t stream): + cdef TVMFFIStreamHandle prev_stream = NULL + CHECK_CALL(TVMFFIEnvSetStream( + device_type, + device_id, + <void*>stream, + &prev_stream)) + return <uint64_t>prev_stream + + +class StreamContext: Review Comment: given this is not cython dependent, move out to stream.py -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
