rllib/policy/torch_policy.py
def export_model(self, export_dir: str, onnx: Optional[int] = None) -> None:
        """Exports the Policy's Model to local directory for serving.

        Creates a TorchScript model and saves it.

        Args:
            export_dir: Local writable directory or filename.
            onnx: If given, will export model in ONNX format. The
                value of this parameter set the ONNX OpSet version to use.
        """
        self._lazy_tensor_dict(self._dummy_batch)
        # Provide dummy state inputs if not an RNN (torch cannot jit with
        # returned empty internal states list).
        if "state_in_0" not in self._dummy_batch:
            self._dummy_batch["state_in_0"] = self._dummy_batch[
                SampleBatch.SEQ_LENS
            ] = np.array([1.0])

        state_ins = []
        i = 0
        while "state_in_{}".format(i) in self._dummy_batch:
            state_ins.append(self._dummy_batch["state_in_{}".format(i)])
            i += 1
        dummy_inputs = {
            k: self._dummy_batch[k]
            for k in self._dummy_batch.keys()
            if k != "is_training"
        }

        if not os.path.exists(export_dir):
            os.makedirs(export_dir)

        seq_lens = self._dummy_batch[SampleBatch.SEQ_LENS]
        if onnx:
            file_name = os.path.join(export_dir, "model.onnx")
            torch.onnx.export(
                self.model,
                (dummy_inputs, state_ins, seq_lens),
                file_name,
                export_params=True,
                opset_version=onnx,
                do_constant_folding=True,
                input_names=list(dummy_inputs.keys())
                + ["state_ins", SampleBatch.SEQ_LENS],
                output_names=["output", "state_outs"],
                dynamic_axes={
                    k: {0: "batch_size"}
                    for k in list(dummy_inputs.keys())
                    + ["state_ins", SampleBatch.SEQ_LENS]
                },
            )
        else:
            traced = torch.jit.trace(self.model, (dummy_inputs, state_ins, seq_lens))
            file_name = os.path.join(export_dir, "model.pt")
            traced.save(file_name)
Exports the Policy's Model to local directory for serving.

        Creates a TorchScript model and saves it.

        Args:
            export_dir: Local writable directory or filename.
            onnx: If given, will export model in ONNX format. The
                value of this parameter set the ONNX OpSet version to use.
        
Exports the Policy's Model to local directory for serving.
Creates a TorchScript model and saves it.
Exports the Policy ' s Model to local directory for serving . Creates a TorchScript model and saves it .
def export_model(self, export_dir: str, onnx: Optional[int] = None) -> None:
        self._lazy_tensor_dict(self._dummy_batch)
        if "state_in_0" not in self._dummy_batch:
            self._dummy_batch["state_in_0"] = self._dummy_batch[
                SampleBatch.SEQ_LENS
            ] = np.array([1.0])
        state_ins = []
        i = 0
        while "state_in_{}".format(i) in self._dummy_batch:
            state_ins.append(self._dummy_batch["state_in_{}".format(i)])
            i += 1
        dummy_inputs = {
            k: self._dummy_batch[k]
            for k in self._dummy_batch.keys()
            if k != "is_training"
        }
        if not os.path.exists(export_dir):
            os.makedirs(export_dir)
        seq_lens = self._dummy_batch[SampleBatch.SEQ_LENS]
        if onnx:
            file_name = os.path.join(export_dir, "model.onnx")
            torch.onnx.export(
                self.model,
                (dummy_inputs, state_ins, seq_lens),
                file_name,
                export_params=True,
                opset_version=onnx,
                do_constant_folding=True,
                input_names=list(dummy_inputs.keys())
                + ["state_ins", SampleBatch.SEQ_LENS],
                output_names=["output", "state_outs"],
                dynamic_axes={
                    k: {0: "batch_size"}
                    for k in list(dummy_inputs.keys())
                    + ["state_ins", SampleBatch.SEQ_LENS]
                },
            )
        else:
            traced = torch.jit.trace(self.model, (dummy_inputs, state_ins, seq_lens))
            file_name = os.path.join(export_dir, "model.pt")
            traced.save(file_name)
def export_model ( self , export_dir : str , onnx : Optional [ int ] = None ) -> None : self . _lazy_tensor_dict ( self . _dummy_batch ) if "state_in_0" not in self . _dummy_batch : self . _dummy_batch [ "state_in_0" ] = self . _dummy_batch [ SampleBatch . SEQ_LENS ] = np . array ( [ 1.0 ] ) state_ins = [ ] i = 0 while "state_in_{}" . format ( i ) in self . _dummy_batch : state_ins . append ( self . _dummy_batch [ "state_in_{}" . format ( i ) ] ) i += 1 dummy_inputs = { k : self . _dummy_batch [ k ] for k in self . _dummy_batch . keys ( ) if k != "is_training" } if not os . path . exists ( export_dir ) : os . makedirs ( export_dir ) seq_lens = self . _dummy_batch [ SampleBatch . SEQ_LENS ] if onnx : file_name = os . path . join ( export_dir , "model.onnx" ) torch . onnx . export ( self . model , ( dummy_inputs , state_ins , seq_lens ) , file_name , export_params = True , opset_version = onnx , do_constant_folding = True , input_names = list ( dummy_inputs . keys ( ) ) + [ "state_ins" , SampleBatch . SEQ_LENS ] , output_names = [ "output" , "state_outs" ] , dynamic_axes = { k : { 0 : "batch_size" } for k in list ( dummy_inputs . keys ( ) ) + [ "state_ins" , SampleBatch . SEQ_LENS ] } , ) else : traced = torch . jit . trace ( self . model , ( dummy_inputs , state_ins , seq_lens ) ) file_name = os . path . join ( export_dir , "model.pt" ) traced . save ( file_name )
Exports the Policy's Model to local directory for serving.
Exports the Policy ' s Model to local directory for serving .
"""Exports the Policy's Model to local directory for serving.

        Creates a TorchScript model and saves it.

        Args:
            export_dir: Local writable directory or filename.
            onnx: If given, will export model in ONNX format. The
                value of this parameter set the ONNX OpSet version to use.
        """ # Provide dummy state inputs if not an RNN (torch cannot jit with # returned empty internal states list).
self
export_dir
str
Local writable directory or filename.
Local writable directory or filename .
onnx
Optional[int]
If given, will export model in ONNX format. The
value of this parameter set the ONNX OpSet version to use.
If given will export model in ONNX format . The value of this parameter set the ONNX OpSet version to use .