Skip to content

Commit

Permalink
Release/0.7.2 (#158)
Browse files Browse the repository at this point in the history
* ENH: Add `Instrument.cpu()` (close #144) (#150)

* ENH: Add `Instrument.cuda()` (close #145) (#151)

* ENH: Add Instrument.float() (close #146) (#153)

* ENH: Add Instrument.double() (close #147) (#154)

* ENH: Add Instrument.half() (close #148) (#155)

* ENH: Add Instrument.bfloat16() (close #149) (#156)
  • Loading branch information
simaki authored Jul 10, 2021
1 parent 9d6f4eb commit ba6c11b
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 4 deletions.
47 changes: 46 additions & 1 deletion pfhedge/instruments/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import torch
from torch import Tensor

T = TypeVar("T")
T = TypeVar("T", bound="Instrument")


class Instrument(ABC):
Expand Down Expand Up @@ -44,6 +44,51 @@ def to(self: T, *args, **kwargs) -> T:
self
"""

def cpu(self: T) -> T:
"""Returns a copy of this object in CPU memory.
If this object is already in CPU memory and on the correct device,
then no copy is performed and the original object is returned.
"""
return self.to(torch.device("cpu"))

def cuda(self: T, device: Optional[int] = None) -> T:
"""Returns a copy of this object in CUDA memory.
If this object is already in CUDA memory and on the correct device,
then no copy is performed and the original object is returned.
Args:
device (torch.device): The destination GPU device. Defaults to the current CUDA device.
"""
return self.to(
torch.device("cuda:{}".format(device) if device is not None else "cuda")
)

def double(self: T) -> T:
"""`self.double()` is equivalent to `self.to(torch.float64)`.
See :func:`to()`.
"""
return self.to(torch.float64)

def float(self: T) -> T:
"""`self.float()` is equivalent to `self.to(torch.float32)`.
See :func:`to()`.
"""
return self.to(torch.float32)

def half(self: T) -> T:
"""`self.half()` is equivalent to `self.to(torch.float16)`.
See :func:`to()`.
"""
return self.to(torch.float16)

def bfloat16(self: T) -> T:
"""`self.bfloat16()` is equivalent to `self.to(torch.bfloat16)`.
See :func:`to()`.
"""
return self.to(torch.bfloat16)

@property
def dinfo(self) -> list:
"""Returns list of strings that tell `dtype` and `device` of `self`.
Expand Down
13 changes: 12 additions & 1 deletion pfhedge/instruments/derivative/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from ..base import Instrument

T = TypeVar("T")
T = TypeVar("T", bound="Derivative")


class Derivative(Instrument):
Expand Down Expand Up @@ -74,4 +74,15 @@ def payoff(self) -> Tensor:
"""


# Assign docstrings so they appear in Sphinx documentation
Derivative.to.__doc__ = Instrument.to.__doc__
Derivative.cpu = Instrument.cpu
Derivative.cpu.__doc__ = Instrument.cpu.__doc__
Derivative.cuda = Instrument.cuda
Derivative.cuda.__doc__ = Instrument.cuda.__doc__
Derivative.double = Instrument.double
Derivative.double.__doc__ = Instrument.double.__doc__
Derivative.float = Instrument.float
Derivative.float.__doc__ = Instrument.float.__doc__
Derivative.half = Instrument.half
Derivative.half.__doc__ = Instrument.half.__doc__
11 changes: 11 additions & 0 deletions pfhedge/instruments/primary/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,15 @@ def to(self: T, *args, **kwargs) -> T:
return self


# Assign docstrings so they appear in Sphinx documentation
Primary.to.__doc__ = Instrument.to.__doc__
Primary.cpu = Instrument.cpu
Primary.cpu.__doc__ = Instrument.cpu.__doc__
Primary.cuda = Instrument.cuda
Primary.cuda.__doc__ = Instrument.cuda.__doc__
Primary.double = Instrument.double
Primary.double.__doc__ = Instrument.double.__doc__
Primary.float = Instrument.float
Primary.float.__doc__ = Instrument.float.__doc__
Primary.half = Instrument.half
Primary.half.__doc__ = Instrument.half.__doc__
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pfhedge"
version = "0.7.1"
version = "0.7.2"
description = "Deep Hedging in PyTorch"
authors = ["Shota Imaki <[email protected]>"]
license = "MIT"
Expand Down
36 changes: 35 additions & 1 deletion tests/instruments/test_underlier.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,55 @@ def test_dtype(self, dtype):
# __init__
s = BrownianStock(dtype=dtype)
s.simulate()
assert s.dtype == dtype
assert s.spot.dtype == dtype

# to() before simulate
s = BrownianStock().to(dtype)
s.simulate()
assert s.dtype == dtype
assert s.spot.dtype == dtype

# to() after simulate
s = BrownianStock()
s.simulate()
s.to(dtype)
assert s.dtype == dtype
assert s.spot.dtype == dtype

s = BrownianStock()
s.simulate()
s.double()
assert s.dtype == torch.float64
assert s.spot.dtype == torch.float64

s = BrownianStock()
s.simulate()
s.float()
assert s.dtype == torch.float32
assert s.spot.dtype == torch.float32

s = BrownianStock()
s.simulate()
s.half()
assert s.dtype == torch.float16
assert s.spot.dtype == torch.float16

s = BrownianStock()
s.simulate()
s.bfloat16()
assert s.dtype == torch.bfloat16
assert s.spot.dtype == torch.bfloat16

def test_device(self):
...
s = BrownianStock(device=torch.device("cuda:0"))
assert s.cpu().device == torch.device("cpu")

def test_cuda(self):
s = BrownianStock()
assert s.cuda(1).device == torch.device("cuda:1")
s = BrownianStock()
assert s.cuda().device == torch.device("cuda")

def test_to_device(self):
s = BrownianStock()
Expand Down

0 comments on commit ba6c11b

Please sign in to comment.