Exemplo n.º 1
0
def test_future_on_chained_exception():
    f = RPCFuture()
    new = f.map(lambda v: v + 2)
    f.set_exception(TExc())

    with pytest.raises(TExc):
        assert new.result()
Exemplo n.º 2
0
    def load_model(self, model: Model, state: ModelState,
                   devices: list) -> RPCFuture[SetDeviceReturnType]:
        log_dir = model.config.get(LOGGING, {}).get(DIRECTORY, "")
        if log_dir:
            os.makedirs(log_dir, exist_ok=True)
            self.logger.info("log dir: %s", os.path.abspath(log_dir))

        self._start_logging_handler()
        incomplete_msg = get_error_msg_for_incomplete_config(model.config)
        if incomplete_msg:
            raise ValueError(incomplete_msg)

        # todo: move test_transforms elsewhere
        self.test_transforms = model.config.get(TESTING, {}).get(
            TRANSFORMS, {"Normalize": {}})

        if not devices:
            devices = ["cpu"]

        cuda_visible_devices, handler_devices = self.get_cuda_and_handler_device_names(
            devices)

        os.environ["CUDA_VISIBLE_DEVICES"] = ",".join(cuda_visible_devices)
        self.logger.info("Set CUDA_VISIBLE_DEVICES to '%s'",
                         os.environ["CUDA_VISIBLE_DEVICES"])

        server_conn, handler_conn = mp.Pipe()
        p = mp.Process(
            target=run_handler,
            name="Handler",
            kwargs={
                "conn": handler_conn,
                "config": model.config,
                "model_file": model.code,
                "model_state": state.model_state,
                "optimizer_state": state.optimizer_state,
                "log_queue": self.log_queue,
            },
        )
        try:
            p.start()
        except Exception as e:
            self.logger.error(e)
            err_fut = RPCFuture()
            err_fut.set_exception(e)
            return err_fut
        else:
            self.handler = create_client(IHandler, server_conn)
            try:
                tik_fut = self.handler.set_devices(handler_devices)
            except Exception as e:
                self.logger.exception("set_devices failed")
                err_fut = RPCFuture()
                err_fut.set_exception(e)
                return err_fut
            else:
                self.logger.info("got tik_fut")
                fut = tik_fut.map(convert_to_SetDeviceReturnType)
                self.logger.info("converted tik_fut")
                return fut