def main(pytorch: bool = False, gpu_id: int = -1): global CONFIG fix_random_seed(0) if gpu_id >= 0: require_gpu(gpu_id) print("Set GPU", gpu_id) backends = {"pytorch": pytorch} for name, use_backend in backends.items(): if not use_backend: print(f"Skipping {name}") continue set_backend(name, gpu_id) C = registry.resolve(Config().from_str(CONFIG)) model = C["model"] X, Y = get_dummy_data(**C["data"]) print("Copy to device") X = [model.ops.asarray(x) for x in X] Y = [model.ops.asarray(y) for y in Y] print("Begin init", len(X)) model.initialize(X=X[:5]) print("Pre-batch") n_words = sum(len(x) for x in X) X = [ model.layers[0].predict(batch) for batch in model.ops.minibatch(16, X) ] model.layers.pop(0) print("Start") start_time = timer() end_time = timer() print(name, n_words, end_time - start_time)
def setup_gpu(use_gpu: int) -> None: """Configure the GPU and log info.""" if use_gpu >= 0: msg.info(f"Using GPU: {use_gpu}") require_gpu(use_gpu) else: msg.info("Using CPU")
def _resolve_gpu(self, use_gpu: int) -> int: if use_gpu >= 0: gpu_id = int(os.environ.get("CUDA_VISIBLE_DEVICES", -1)) logger.info(f"Using GPU (isolated): {gpu_id}") require_gpu(0) else: logger.info("Using CPU") gpu_id = -1 return gpu_id
def setup_gpu(use_gpu: int) -> None: """Configure the GPU and log info.""" if use_gpu >= 0: msg.info(f"Using GPU: {use_gpu}") require_gpu(use_gpu) else: msg.info("Using CPU") if has_cupy and gpu_is_available(): msg.info("To switch to GPU 0, use the option: --gpu-id 0")
def thread_training(training_step_iterator, print_row, rank, num_workers, gpu_id): if gpu_id >= 0: # I don't fully understand why we need to do this within the thread. # I think 0 is also correct here, because ray sets the available devices? require_gpu(0) for batch, info, is_best_checkpoint in training_step_iterator: if rank == 0 and is_best_checkpoint is not None: info["words"] *= num_workers print_row(info)
def setup_gpu(use_gpu: int, silent=None) -> None: """Configure the GPU and log info.""" if silent is None: local_msg = Printer() else: local_msg = Printer(no_print=silent, pretty=not silent) if use_gpu >= 0: local_msg.info(f"Using GPU: {use_gpu}") require_gpu(use_gpu) else: local_msg.info("Using CPU") if gpu_is_available(): local_msg.info("To switch to GPU 0, use the option: --gpu-id 0")
def main(numpy: bool = False, pytorch: bool = False, generic: bool = False, gpu_id: int = -1): global CONFIG fix_random_seed(0) if gpu_id >= 0: require_gpu(gpu_id) print("Set GPU", gpu_id) backends = {"pytorch": pytorch, "numpy": numpy, "generic": generic} for name, use_backend in backends.items(): if not use_backend: print(f"Skipping {name}") continue set_backend(name, gpu_id) print("Getting data") C = registry.resolve(Config().from_str(CONFIG)) model = C["model"] X, Y = get_dummy_data(**C["data"]) print("Copy to device") X = [model.ops.asarray(x) for x in X] Y = [model.ops.asarray(y) for y in Y] print("Begin init", len(X)) model.initialize(X=X[:5]) print("Pre-batch") n_words = sum(len(x) for x in X) batches = model.ops.multibatch(16, X, Y) batches = [(model.layers[0].predict(x), y) for x, y in batches] model.layers.pop(0) print("Start") start_time = timer() total = run_forward(model, [x for x, y in batches]) end_time = timer() print(name, n_words, total, end_time - start_time) start_time = timer() total = run_forward_backward(model, batches) end_time = timer() print(name, n_words, total, end_time - start_time)
import spacy from copy import copy from spacy_transformers.pipeline_component import DEFAULT_CONFIG from thinc.api import Config, set_gpu_allocator, require_gpu if spacy.prefer_gpu(): print("\n\033[92m" + "✔ Using GPU" + "\033[0m\n") set_gpu_allocator("pytorch") require_gpu(0) else: print("\n\033[91m" + "✘ NOT Using GPU!" + "\033[0m\n") config = copy(DEFAULT_CONFIG["transformer"]) config["model"]["name"] = "model/distilbert-base-nli-stsb-mean-tokens" nlp = spacy.blank("en") transformer = nlp.add_pipe("transformer", config=config) transformer.model.initialize() doc = nlp("hello world") tokvecs = doc._.trf_data.tensors[-1] print(tokvecs)