-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b9f1b2f
commit 343e200
Showing
5 changed files
with
79 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use candle_core::{Result, Tensor}; | ||
|
||
use crate::EmbeddingLayerLike; | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct FrozenEmbedding { | ||
embeddings: Tensor, | ||
hidden_size: usize, | ||
} | ||
|
||
impl FrozenEmbedding { | ||
pub(crate) fn new(embeddings: &Tensor, hidden_size: usize) -> Result<Self> { | ||
Check failure on line 12 in src/frozenembed.rs
|
||
Ok(Self { | ||
embeddings: embeddings.detach()?, | ||
hidden_size, | ||
}) | ||
} | ||
|
||
pub(crate) fn new_from_linear(old: &dyn EmbeddingLayerLike) -> Result<Self> { | ||
Self::new(old.embeddings(), old.hidden_size()) | ||
} | ||
} | ||
|
||
impl crate::Module for FrozenEmbedding { | ||
fn forward(&self, indexes: &Tensor) -> Result<Tensor> { | ||
let mut final_dims = indexes.dims().to_vec(); | ||
final_dims.push(self.hidden_size); | ||
let indexes = indexes.flatten_all()?; | ||
let values = self.embeddings.index_select(&indexes, 0)?; | ||
let values = values.reshape(final_dims)?; | ||
Ok(values) | ||
} | ||
} | ||
|
||
impl EmbeddingLayerLike for FrozenEmbedding { | ||
fn embeddings(&self) -> &Tensor { | ||
&self.embeddings | ||
} | ||
fn hidden_size(&self) -> usize { | ||
self.hidden_size | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters