code stringlengths 66 870k | docstring stringlengths 19 26.7k | func_name stringlengths 1 138 | language stringclasses 1
value | repo stringlengths 7 68 | path stringlengths 5 324 | url stringlengths 46 389 | license stringclasses 7
values |
|---|---|---|---|---|---|---|---|
def pad_to_len(
arr: torch.tensor,
target_len: int,
*,
left_pad: bool,
eos_token: int,
device: torch.device,
) -> torch.tensor:
"""Pad or truncate array to given length."""
if arr.shape[1] < target_len:
shape_for_ones = list(arr.shape)
shape_for_ones[1] = target_len - shape_for_ones[... | Pad or truncate array to given length. | pad_to_len | python | google-deepmind/synthid-text | src/synthid_text/detector_bayesian.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/detector_bayesian.py | Apache-2.0 |
def filter_and_truncate(
outputs: torch.tensor,
truncation_length: Optional[int],
eos_token_mask: torch.tensor,
) -> torch.tensor:
"""Filter and truncate outputs to given length.
Args:
outputs: output tensor of shape [batch_size, output_len]
truncation_length: Length to truncate the final output.... | Filter and truncate outputs to given length.
Args:
outputs: output tensor of shape [batch_size, output_len]
truncation_length: Length to truncate the final output. If None, then no
truncation is applied.
eos_token_mask: EOS token mask of shape [batch_size, output_len]
Returns:
output tensor of sh... | filter_and_truncate | python | google-deepmind/synthid-text | src/synthid_text/detector_bayesian.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/detector_bayesian.py | Apache-2.0 |
def process_outputs_for_training(
all_outputs: Sequence[torch.Tensor],
logits_processor: logits_processing.SynthIDLogitsProcessor,
tokenizer: Any,
*,
pos_truncation_length: Optional[int],
neg_truncation_length: Optional[int],
max_length: int,
is_cv: bool,
is_pos: bool,
torch_devi... | Process raw model outputs into format understandable by the detector.
Args:
all_outputs: sequence of outputs of shape [batch_size, output_len].
logits_processor: logits processor used for watermarking.
tokenizer: tokenizer used for the model.
pos_truncation_length: Length to truncate the watermarked outp... | process_outputs_for_training | python | google-deepmind/synthid-text | src/synthid_text/detector_bayesian.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/detector_bayesian.py | Apache-2.0 |
def __call__(self, g_values: jnp.ndarray) -> jnp.ndarray:
"""Computes likelihoods given g-values and a mask.
Args:
g_values: g-values (all are 0 or 1) of shape [batch_size, seq_len,
watermarking_depth, ...].
Returns:
an array of shape [batch_size, seq_len, watermarking_depth] or
... | Computes likelihoods given g-values and a mask.
Args:
g_values: g-values (all are 0 or 1) of shape [batch_size, seq_len,
watermarking_depth, ...].
Returns:
an array of shape [batch_size, seq_len, watermarking_depth] or
[batch_size, seq_len, 1] corresponding to the likelihoods
o... | __call__ | python | google-deepmind/synthid-text | src/synthid_text/detector_bayesian.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/detector_bayesian.py | Apache-2.0 |
def _compute_latents(
self, g_values: jnp.ndarray
) -> tuple[jnp.ndarray, jnp.ndarray]:
"""Computes the unique token probability distribution given g-values.
Args:
g_values: Pseudorandom function values of shape [batch_size, seq_len,
watermarking_depth].
Returns:
p_one_unique_t... | Computes the unique token probability distribution given g-values.
Args:
g_values: Pseudorandom function values of shape [batch_size, seq_len,
watermarking_depth].
Returns:
p_one_unique_token and p_two_unique_tokens, both of shape
[batch_size, seq_len, watermarking_depth]. p_one_un... | _compute_latents | python | google-deepmind/synthid-text | src/synthid_text/detector_bayesian.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/detector_bayesian.py | Apache-2.0 |
def __call__(self, g_values: jnp.ndarray) -> jnp.ndarray:
"""Computes the likelihoods P(g_values|watermarked).
Args:
g_values: g-values (values 0 or 1) of shape [batch_size, seq_len,
watermarking_depth]
Returns:
p(g_values|watermarked) of shape [batch_size, seq_len,
watermarking_... | Computes the likelihoods P(g_values|watermarked).
Args:
g_values: g-values (values 0 or 1) of shape [batch_size, seq_len,
watermarking_depth]
Returns:
p(g_values|watermarked) of shape [batch_size, seq_len,
watermarking_depth].
| __call__ | python | google-deepmind/synthid-text | src/synthid_text/detector_bayesian.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/detector_bayesian.py | Apache-2.0 |
def _compute_posterior(
likelihoods_watermarked: jnp.ndarray,
likelihoods_unwatermarked: jnp.ndarray,
mask: jnp.ndarray,
prior: float,
) -> jnp.ndarray:
"""Compute posterior P(w|g) given likelihoods, mask and prior.
Args:
likelihoods_watermarked: shape [batch, length, depth]. Likelihoods
... | Compute posterior P(w|g) given likelihoods, mask and prior.
Args:
likelihoods_watermarked: shape [batch, length, depth]. Likelihoods
P(g_values|watermarked) of g-values under watermarked model.
likelihoods_unwatermarked: shape [batch, length, depth]. Likelihoods
P(g_values|unwatermarked) of g-val... | _compute_posterior | python | google-deepmind/synthid-text | src/synthid_text/detector_bayesian.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/detector_bayesian.py | Apache-2.0 |
def __call__(
self,
g_values: jnp.ndarray,
mask: jnp.ndarray,
) -> jnp.ndarray:
"""Computes the watermarked posterior P(watermarked|g_values).
Args:
g_values: g-values (with values 0 or 1) of shape [batch_size, seq_len,
watermarking_depth, ...]
mask: A binary array shape... | Computes the watermarked posterior P(watermarked|g_values).
Args:
g_values: g-values (with values 0 or 1) of shape [batch_size, seq_len,
watermarking_depth, ...]
mask: A binary array shape [batch_size, seq_len] indicating which g-values
should be used. g-values with mask value 0 are dis... | __call__ | python | google-deepmind/synthid-text | src/synthid_text/detector_bayesian.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/detector_bayesian.py | Apache-2.0 |
def loss_fn(
params: Mapping[str, Any],
detector_inputs: Any,
w_true: jnp.ndarray,
l2_batch_weight: float,
detector_module: BayesianDetectorModule,
) -> jnp.ndarray:
"""Calculates loss for a batch of data given parameters."""
w_pred = detector_module.apply(
params, *detector_inputs, method... | Calculates loss for a batch of data given parameters. | loss_fn | python | google-deepmind/synthid-text | src/synthid_text/detector_bayesian.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/detector_bayesian.py | Apache-2.0 |
def train(
*,
detector_module: BayesianDetectorModule,
g_values: jnp.ndarray,
mask: jnp.ndarray,
watermarked: jnp.ndarray,
epochs: int = 250,
learning_rate: float = 1e-3,
minibatch_size: int = 64,
seed: int = 0,
l2_weight: float = 0.0,
shuffle: bool = True,
g_values_val: ... | Trains a Bayesian detector model.
Args:
detector_module: The detector module to train in-place.
g_values: g-values of shape [num_train, seq_len, watermarking_depth].
mask: A binary array shape [num_train, seq_len] indicating which g-values
should be used. g-values with mask value 0 are discarded.
... | train | python | google-deepmind/synthid-text | src/synthid_text/detector_bayesian.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/detector_bayesian.py | Apache-2.0 |
def update_fn_if_fpr_tpr(params):
"""Loss function for negative TPR@FPR=1% as the validation loss."""
tpr_ = tpr_at_fpr(
params=params,
detector_inputs=(g_values_val, mask_val),
w_true=watermarked_val,
minibatch_size=minibatch_size,
detector_module=detector_module,
)
... | Loss function for negative TPR@FPR=1% as the validation loss. | update_fn_if_fpr_tpr | python | google-deepmind/synthid-text | src/synthid_text/detector_bayesian.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/detector_bayesian.py | Apache-2.0 |
def update_with_minibatches(gvalues, masks, labels, inds, params, opt_state):
"""Update params iff opt_state is not None and always returns the loss."""
losses = []
for start in inds:
end = start + minibatch_size
loss, params, opt_state = update(
gvalues[start:end],
masks[sta... | Update params iff opt_state is not None and always returns the loss. | update_with_minibatches | python | google-deepmind/synthid-text | src/synthid_text/detector_bayesian.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/detector_bayesian.py | Apache-2.0 |
def update_fn(opt_state, params):
"""Updates the model parameters and returns the loss."""
loss, params, opt_state = update_with_minibatches(
g_values, mask, watermarked, minibatch_inds, params, opt_state
)
val_loss = None
if g_values_val is not None:
if validation_metric == Validation... | Updates the model parameters and returns the loss. | update_fn | python | google-deepmind/synthid-text | src/synthid_text/detector_bayesian.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/detector_bayesian.py | Apache-2.0 |
def score(self, outputs: jnp.ndarray) -> jnp.ndarray:
"""Score the model output for possibility of being watermarked.
Score is within [0, 1] where 0 is not watermarked and 1 is watermarked.
Args:
outputs: model output of shape [batch_size, output_len]
Returns:
scores of shape [batch_size]... | Score the model output for possibility of being watermarked.
Score is within [0, 1] where 0 is not watermarked and 1 is watermarked.
Args:
outputs: model output of shape [batch_size, output_len]
Returns:
scores of shape [batch_size]
| score | python | google-deepmind/synthid-text | src/synthid_text/detector_bayesian.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/detector_bayesian.py | Apache-2.0 |
def process_raw_model_outputs(
cls,
*,
tokenized_wm_outputs: Union[Sequence[np.ndarray], np.ndarray],
tokenized_uwm_outputs: Union[Sequence[np.ndarray], np.ndarray],
logits_processor: logits_processing.SynthIDLogitsProcessor,
tokenizer: Any,
torch_device: torch.device,
te... | Process raw models outputs into inputs we can train.
Args:
tokenized_wm_outputs: tokenized outputs of watermarked data.
tokenized_uwm_outputs: tokenized outputs of unwatermarked data.
logits_processor: logits processor used for watermarking.
tokenizer: tokenizer used for the model.
to... | process_raw_model_outputs | python | google-deepmind/synthid-text | src/synthid_text/detector_bayesian.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/detector_bayesian.py | Apache-2.0 |
def train_best_detector_given_g_values(
cls,
*,
train_g_values: jnp.ndarray,
train_masks: jnp.ndarray,
train_labels: jnp.ndarray,
cv_g_values: jnp.ndarray,
cv_masks: jnp.ndarray,
cv_labels: jnp.ndarray,
logits_processor: logits_processing.SynthIDLogitsProcessor,
... | Train best detector given g_values, mask and labels. | train_best_detector_given_g_values | python | google-deepmind/synthid-text | src/synthid_text/detector_bayesian.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/detector_bayesian.py | Apache-2.0 |
def train_best_detector(
cls,
*,
tokenized_wm_outputs: Union[Sequence[np.ndarray], np.ndarray],
tokenized_uwm_outputs: Union[Sequence[np.ndarray], np.ndarray],
logits_processor: logits_processing.SynthIDLogitsProcessor,
tokenizer: Any,
torch_device: torch.device,
test_siz... | Construct, train and return the best detector based on wm and uwm data.
In practice, we have found that tuning pos_truncation_length,
neg_truncation_length, n_epochs, learning_rate and l2_weights can help
improve the performance of the detector. We recommend tuning these
parameters for your data.
... | train_best_detector | python | google-deepmind/synthid-text | src/synthid_text/detector_bayesian.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/detector_bayesian.py | Apache-2.0 |
def mean_score(
g_values: jnp.ndarray,
mask: jnp.ndarray,
) -> jnp.ndarray:
"""Computes the Mean score.
Args:
g_values: g-values of shape [batch_size, seq_len, watermarking_depth].
mask: A binary array shape [batch_size, seq_len] indicating which g-values
should be used. g-values with mask va... | Computes the Mean score.
Args:
g_values: g-values of shape [batch_size, seq_len, watermarking_depth].
mask: A binary array shape [batch_size, seq_len] indicating which g-values
should be used. g-values with mask value 0 are discarded.
Returns:
Mean scores, of shape [batch_size]. This is the mean... | mean_score | python | google-deepmind/synthid-text | src/synthid_text/detector_mean.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/detector_mean.py | Apache-2.0 |
def weighted_mean_score(
g_values: jnp.ndarray,
mask: jnp.ndarray,
weights: Optional[jnp.ndarray] = None,
) -> jnp.ndarray:
"""Computes the Weighted Mean score.
Args:
g_values: g-values of shape [batch_size, seq_len, watermarking_depth].
mask: A binary array shape [batch_size, seq_len] indicati... | Computes the Weighted Mean score.
Args:
g_values: g-values of shape [batch_size, seq_len, watermarking_depth].
mask: A binary array shape [batch_size, seq_len] indicating which g-values
should be used. g-values with mask value 0 are discarded.
weights: array of non-negative floats, shape [watermark... | weighted_mean_score | python | google-deepmind/synthid-text | src/synthid_text/detector_mean.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/detector_mean.py | Apache-2.0 |
def expected_mean_g_value(
vocab_size: int,
num_leaves: int = 2,
) -> float:
"""Compute expected mean g-value after watermarking, assuming uniform LM dist.
This is the theoretical expected value for a single-layer of tournament
watermarking, using a Bernoulli(0.5) g-value distribution and N=num_leaves
... | Compute expected mean g-value after watermarking, assuming uniform LM dist.
This is the theoretical expected value for a single-layer of tournament
watermarking, using a Bernoulli(0.5) g-value distribution and N=num_leaves
samples, assuming that the LM distribution p_LM is uniform.
Args:
vocab_size: The s... | expected_mean_g_value | python | google-deepmind/synthid-text | src/synthid_text/g_value_expectations.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/g_value_expectations.py | Apache-2.0 |
def accumulate_hash(
current_hash: torch.LongTensor,
data: torch.LongTensor,
multiplier: int = 6364136223846793005,
increment: int = 1,
) -> torch.LongTensor:
"""Accumulate hash of data on current hash.
Method uses adapted linear congruential generator with newlib/musl parameters.
This function ... | Accumulate hash of data on current hash.
Method uses adapted linear congruential generator with newlib/musl parameters.
This function has following property -
f(x, data[T]) = f(f(x, data[:T - 1]), data[T])
This function expects current_hash.shape and data.shape[:-1] to
match/broadcastable.
Args:
cur... | accumulate_hash | python | google-deepmind/synthid-text | src/synthid_text/hashing_function.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/hashing_function.py | Apache-2.0 |
def update_scores(
scores: torch.FloatTensor,
g_values: torch.FloatTensor,
) -> torch.FloatTensor:
"""Updates scores using the g values.
We assume that the scores are in the log space.
Args:
scores: Scores (batch_size, vocab_size).
g_values: G values (batch_size, vocab_size, depth).
Returns:
... | Updates scores using the g values.
We assume that the scores are in the log space.
Args:
scores: Scores (batch_size, vocab_size).
g_values: G values (batch_size, vocab_size, depth).
Returns:
Updated scores (batch_size, vocab_size).
| update_scores | python | google-deepmind/synthid-text | src/synthid_text/logits_processing.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/logits_processing.py | Apache-2.0 |
def update_scores_distortionary(
scores: torch.FloatTensor,
g_values: torch.FloatTensor,
num_leaves: int,
) -> torch.FloatTensor:
"""Update scores using the g values for distortionary tournament watermarking.
We assume that the scores are in the log space.
Args:
scores: Scores (batch_size, vocab_... | Update scores using the g values for distortionary tournament watermarking.
We assume that the scores are in the log space.
Args:
scores: Scores (batch_size, vocab_size).
g_values: G values (batch_size, vocab_size, depth).
num_leaves: Number of leaves per node in the tournament tree.
Returns:
Up... | update_scores_distortionary | python | google-deepmind/synthid-text | src/synthid_text/logits_processing.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/logits_processing.py | Apache-2.0 |
def __init__(
self,
batch_size: int,
ngram_len: int,
context_history_size: int,
device: torch.device,
):
"""Initializes the state.
Args:
batch_size: Batch size.
ngram_len: Ngram length.
context_history_size: Size of the tensor to keep track of seen contexts.
... | Initializes the state.
Args:
batch_size: Batch size.
ngram_len: Ngram length.
context_history_size: Size of the tensor to keep track of seen contexts.
device: Device to use.
| __init__ | python | google-deepmind/synthid-text | src/synthid_text/logits_processing.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/logits_processing.py | Apache-2.0 |
def __init__(
self,
*,
ngram_len: int,
keys: Sequence[int],
sampling_table_size: int,
sampling_table_seed: int,
context_history_size: int,
temperature: float,
top_k: int,
device: torch.device,
skip_first_ngram_calls: bool = False,
apply_top_k: bool... | Initializes the logits processor.
Args:
ngram_len: Ngram length.
keys: A sequence of watermarking keys, one for each depth.
sampling_table_size: Size of the sampling table.
sampling_table_seed: Random seed to generate the sampling table.
context_history_size: Size of the tensor to kee... | __init__ | python | google-deepmind/synthid-text | src/synthid_text/logits_processing.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/logits_processing.py | Apache-2.0 |
def watermarked_call(
self,
input_ids: torch.LongTensor,
scores: torch.FloatTensor,
) -> tuple[torch.FloatTensor, torch.LongTensor, torch.FloatTensor]:
"""Calls the logits processor statefully.
This function computes top_k internally and returns the indices mapping
from top_k scores to ... | Calls the logits processor statefully.
This function computes top_k internally and returns the indices mapping
from top_k scores to dense scores.
Args:
input_ids: Input token ids (batch_size, inputs_len).
scores: Scores (batch_size, vocab_size).
Returns:
Tuple of
Watermarked... | watermarked_call | python | google-deepmind/synthid-text | src/synthid_text/logits_processing.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/logits_processing.py | Apache-2.0 |
def compute_ngram_keys(
self,
ngrams: torch.LongTensor,
) -> torch.LongTensor:
"""Computes random keys for each ngram and depth.
Args:
ngrams: Ngrams (batch_size, num_ngrams, ngram_len).
Returns:
ngram keys (batch_size, num_ngrams, depth).
"""
if len(ngrams.shape) != 3:
... | Computes random keys for each ngram and depth.
Args:
ngrams: Ngrams (batch_size, num_ngrams, ngram_len).
Returns:
ngram keys (batch_size, num_ngrams, depth).
| compute_ngram_keys | python | google-deepmind/synthid-text | src/synthid_text/logits_processing.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/logits_processing.py | Apache-2.0 |
def _compute_keys(
self,
n_minus_1_grams: torch.LongTensor,
indices: torch.LongTensor,
) -> tuple[torch.LongTensor, torch.LongTensor]:
"""Computes random keys for each ngram and depth.
Args:
n_minus_1_grams: Ngrams (batch_size, ngram_len - 1).
indices: indices of the continuatio... | Computes random keys for each ngram and depth.
Args:
n_minus_1_grams: Ngrams (batch_size, ngram_len - 1).
indices: indices of the continuations (batch_size, num_indices)
Returns:
Ngram keys (batch_size, num_indices, depth).
| _compute_keys | python | google-deepmind/synthid-text | src/synthid_text/logits_processing.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/logits_processing.py | Apache-2.0 |
def sample_g_values(self, ngram_keys: torch.LongTensor) -> torch.LongTensor:
"""Samples g values from Bernoulli distribution.
It is not possible to pass random keys in a vectorized way in torch. Instead
we pre-compute a random sampling table, and use apply modulo table size to
map from ngram keys (int6... | Samples g values from Bernoulli distribution.
It is not possible to pass random keys in a vectorized way in torch. Instead
we pre-compute a random sampling table, and use apply modulo table size to
map from ngram keys (int64) to g values.
Args:
ngram_keys: Random keys (batch_size, num_ngrams, de... | sample_g_values | python | google-deepmind/synthid-text | src/synthid_text/logits_processing.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/logits_processing.py | Apache-2.0 |
def _check_input_ids_shape(self, input_ids: torch.LongTensor):
"""Checks the shape of input ids."""
if len(input_ids.shape) != 2:
raise ValueError(
"Input ids should be of shape (batch_size, input_len), but is"
f" {input_ids.shape}"
) | Checks the shape of input ids. | _check_input_ids_shape | python | google-deepmind/synthid-text | src/synthid_text/logits_processing.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/logits_processing.py | Apache-2.0 |
def compute_g_values(
self,
input_ids: torch.LongTensor,
) -> torch.LongTensor:
"""Computes g values for each ngram from the given sequence of tokens.
Args:
input_ids: Input token ids (batch_size, input_len).
Returns:
G values (batch_size, input_len - (ngram_len - 1), depth).
... | Computes g values for each ngram from the given sequence of tokens.
Args:
input_ids: Input token ids (batch_size, input_len).
Returns:
G values (batch_size, input_len - (ngram_len - 1), depth).
| compute_g_values | python | google-deepmind/synthid-text | src/synthid_text/logits_processing.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/logits_processing.py | Apache-2.0 |
def compute_context_repetition_mask(
self,
input_ids: torch.LongTensor,
) -> torch.LongTensor:
"""Computes repetition mask.
0 and 1 stand for repeated and not repeated context n-1 grams respectively.
Args:
input_ids: Input token ids (batch_size, input_len).
Returns:
Repetiti... | Computes repetition mask.
0 and 1 stand for repeated and not repeated context n-1 grams respectively.
Args:
input_ids: Input token ids (batch_size, input_len).
Returns:
Repetitions mask (batch_size, input_len - (ngram_len - 1)).
| compute_context_repetition_mask | python | google-deepmind/synthid-text | src/synthid_text/logits_processing.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/logits_processing.py | Apache-2.0 |
def compute_eos_token_mask(
self,
input_ids: torch.LongTensor,
eos_token_id: int,
) -> torch.LongTensor:
"""Computes repetitions mask.
1 stands for ngrams that don't contain EOS tokens and vice versa.
Args:
input_ids: Input token ids (batch_size, input_len).
eos_token_id: E... | Computes repetitions mask.
1 stands for ngrams that don't contain EOS tokens and vice versa.
Args:
input_ids: Input token ids (batch_size, input_len).
eos_token_id: EOS token ID.
Returns:
EOS token mask (batch_size, input_len).
| compute_eos_token_mask | python | google-deepmind/synthid-text | src/synthid_text/logits_processing.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/logits_processing.py | Apache-2.0 |
def does_mean_g_value_matches_theoretical(
vocab_size: int,
ngram_len: int,
batch_size: int,
keys: Sequence[int],
atol: float,
device: torch.device,
num_leaves: int = 2,
) -> tuple[float, float, bool]:
"""Tests that the mean g-value is close to theoretical value.
SynthIDLogitsProcessor ... | Tests that the mean g-value is close to theoretical value.
SynthIDLogitsProcessor is tested on its own using random input tokens.
Args:
vocab_size: vocab size of the model.
ngram_len: length of the ngram.
batch_size: batch size of the model.
keys: keys used for watermarking.
atol: absolute tol... | does_mean_g_value_matches_theoretical | python | google-deepmind/synthid-text | src/synthid_text/logits_processing_test.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/logits_processing_test.py | Apache-2.0 |
def test_distributional_convergence(self):
"""Check if watermarked distribution converges to input distribution."""
vocab_size = 2
batch_size = 1500
num_keys = 1000
device = torch_testing.torch_device()
temperature = 1.0
updated_softmaxes = 0
for _ in tqdm.tqdm(range(num_keys)):
w... | Check if watermarked distribution converges to input distribution. | test_distributional_convergence | python | google-deepmind/synthid-text | src/synthid_text/logits_processing_test.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/logits_processing_test.py | Apache-2.0 |
def set_up_logits_processor(
self,
batch_size,
sequence_len,
num_layers,
ngram_len,
top_k,
vocab_size,
):
"""Setup function for all the tests."""
device = torch_testing.torch_device()
watermarking_config = immutabledict.immutabledict({
'ngram_len': ngram_l... | Setup function for all the tests. | set_up_logits_processor | python | google-deepmind/synthid-text | src/synthid_text/logits_processing_test.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/logits_processing_test.py | Apache-2.0 |
def _get_logits_warper(
self,
generation_config: transformers.GenerationConfig,
**unused_kw,
) -> transformers.LogitsProcessorList:
"""Constructs and returns a list of warpers.
This overrides the base class's implementation to control how we apply top_k
and temperature. Only the SynthID... | Constructs and returns a list of warpers.
This overrides the base class's implementation to control how we apply top_k
and temperature. Only the SynthIDLogitsProcessor warper is constructed that
performs top_k and temperature scaling before applying watermark. This is
to improve the latency impact by w... | _get_logits_warper | python | google-deepmind/synthid-text | src/synthid_text/synthid_mixin.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/synthid_mixin.py | Apache-2.0 |
def _sample(
self,
input_ids: torch.LongTensor,
logits_processor: transformers.LogitsProcessorList,
stopping_criteria: transformers.StoppingCriteriaList,
generation_config: transformers.GenerationConfig,
synced_gpus: bool,
streamer: Optional["transformers.BaseStreamer"],
... | Sample sequence of tokens.
Generates sequences of token ids for models with a language modeling head
using **multinomial sampling** and
can be used for text-decoder, text-to-text, speech-to-text, and
vision-to-text models.
This function is copied and changed minimally from the HuggingFace
repo... | _sample | python | google-deepmind/synthid-text | src/synthid_text/synthid_mixin.py | https://github.com/google-deepmind/synthid-text/blob/master/src/synthid_text/synthid_mixin.py | Apache-2.0 |
def get_root():
"""Get the project root directory.
We require that all commands are run from the project root, i.e. the
directory that contains setup.py, setup.cfg, and versioneer.py .
"""
root = os.path.realpath(os.path.abspath(os.getcwd()))
setup_py = os.path.join(root, "setup.py")
versio... | Get the project root directory.
We require that all commands are run from the project root, i.e. the
directory that contains setup.py, setup.cfg, and versioneer.py .
| get_root | python | palantir/python-language-server | versioneer.py | https://github.com/palantir/python-language-server/blob/master/versioneer.py | MIT |
def get_config_from_root(root):
"""Read the project setup.cfg file to determine Versioneer config."""
# This might raise EnvironmentError (if setup.cfg is missing), or
# configparser.NoSectionError (if it lacks a [versioneer] section), or
# configparser.NoOptionError (if it lacks "VCS="). See the docstr... | Read the project setup.cfg file to determine Versioneer config. | get_config_from_root | python | palantir/python-language-server | versioneer.py | https://github.com/palantir/python-language-server/blob/master/versioneer.py | MIT |
def register_vcs_handler(vcs, method): # decorator
"""Decorator to mark a method as the handler for a particular VCS."""
def decorate(f):
"""Store f in HANDLERS[vcs][method]."""
if vcs not in HANDLERS:
HANDLERS[vcs] = {}
HANDLERS[vcs][method] = f
return f
return ... | Decorator to mark a method as the handler for a particular VCS. | register_vcs_handler | python | palantir/python-language-server | versioneer.py | https://github.com/palantir/python-language-server/blob/master/versioneer.py | MIT |
def git_get_keywords(versionfile_abs):
"""Extract version information from the given file."""
# the code embedded in _version.py can just fetch the value of these
# keywords. When used from setup.py, we don't want to import _version.py,
# so we do it with a regexp instead. This function is not used from... | Extract version information from the given file. | git_get_keywords | python | palantir/python-language-server | versioneer.py | https://github.com/palantir/python-language-server/blob/master/versioneer.py | MIT |
def git_versions_from_keywords(keywords, tag_prefix, verbose):
"""Get version information from git keywords."""
if not keywords:
raise NotThisMethod("no keywords at all, weird")
date = keywords.get("date")
if date is not None:
# git-2.2.0 added "%cI", which expands to an ISO-8601 -compli... | Get version information from git keywords. | git_versions_from_keywords | python | palantir/python-language-server | versioneer.py | https://github.com/palantir/python-language-server/blob/master/versioneer.py | MIT |
def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):
"""Get version from 'git describe' in the root of the source tree.
This only gets called if the git-archive 'subst' keywords were *not*
expanded, and _version.py hasn't already been rewritten with a short
version string, meani... | Get version from 'git describe' in the root of the source tree.
This only gets called if the git-archive 'subst' keywords were *not*
expanded, and _version.py hasn't already been rewritten with a short
version string, meaning we're inside a checked out source tree.
| git_pieces_from_vcs | python | palantir/python-language-server | versioneer.py | https://github.com/palantir/python-language-server/blob/master/versioneer.py | MIT |
def do_vcs_install(manifest_in, versionfile_source, ipy):
"""Git-specific installation logic for Versioneer.
For Git, this means creating/changing .gitattributes to mark _version.py
for export-subst keyword substitution.
"""
GITS = ["git"]
if sys.platform == "win32":
GITS = ["git.cmd", ... | Git-specific installation logic for Versioneer.
For Git, this means creating/changing .gitattributes to mark _version.py
for export-subst keyword substitution.
| do_vcs_install | python | palantir/python-language-server | versioneer.py | https://github.com/palantir/python-language-server/blob/master/versioneer.py | MIT |
def versions_from_parentdir(parentdir_prefix, root, verbose):
"""Try to determine the version from the parent directory name.
Source tarballs conventionally unpack into a directory that includes both
the project name and a version string. We will also support searching up
two directory levels for an ap... | Try to determine the version from the parent directory name.
Source tarballs conventionally unpack into a directory that includes both
the project name and a version string. We will also support searching up
two directory levels for an appropriately named parent directory
| versions_from_parentdir | python | palantir/python-language-server | versioneer.py | https://github.com/palantir/python-language-server/blob/master/versioneer.py | MIT |
def versions_from_file(filename):
"""Try to determine the version from _version.py if present."""
try:
with open(filename) as f:
contents = f.read()
except EnvironmentError:
raise NotThisMethod("unable to read _version.py")
mo = re.search(r"version_json = '''\n(.*)''' # END ... | Try to determine the version from _version.py if present. | versions_from_file | python | palantir/python-language-server | versioneer.py | https://github.com/palantir/python-language-server/blob/master/versioneer.py | MIT |
def write_to_version_file(filename, versions):
"""Write the given version number to the given _version.py file."""
os.unlink(filename)
contents = json.dumps(versions, sort_keys=True,
indent=1, separators=(",", ": "))
with open(filename, "w") as f:
f.write(SHORT_VERSION_... | Write the given version number to the given _version.py file. | write_to_version_file | python | palantir/python-language-server | versioneer.py | https://github.com/palantir/python-language-server/blob/master/versioneer.py | MIT |
def plus_or_dot(pieces):
"""Return a + if we don't already have one, else return a ."""
if "+" in pieces.get("closest-tag", ""):
return "."
return "+" | Return a + if we don't already have one, else return a . | plus_or_dot | python | palantir/python-language-server | versioneer.py | https://github.com/palantir/python-language-server/blob/master/versioneer.py | MIT |
def render_pep440(pieces):
"""Build up version string, with post-release "local version identifier".
Our goal: TAG[+DISTANCE.gHEX[.dirty]] . Note that if you
get a tagged build and then dirty it, you'll get TAG+0.gHEX.dirty
Exceptions:
1: no tags. git_describe was just HEX. 0+untagged.DISTANCE.gHE... | Build up version string, with post-release "local version identifier".
Our goal: TAG[+DISTANCE.gHEX[.dirty]] . Note that if you
get a tagged build and then dirty it, you'll get TAG+0.gHEX.dirty
Exceptions:
1: no tags. git_describe was just HEX. 0+untagged.DISTANCE.gHEX[.dirty]
| render_pep440 | python | palantir/python-language-server | versioneer.py | https://github.com/palantir/python-language-server/blob/master/versioneer.py | MIT |
def render_pep440_pre(pieces):
"""TAG[.post.devDISTANCE] -- No -dirty.
Exceptions:
1: no tags. 0.post.devDISTANCE
"""
if pieces["closest-tag"]:
rendered = pieces["closest-tag"]
if pieces["distance"]:
rendered += ".post.dev%d" % pieces["distance"]
else:
# exce... | TAG[.post.devDISTANCE] -- No -dirty.
Exceptions:
1: no tags. 0.post.devDISTANCE
| render_pep440_pre | python | palantir/python-language-server | versioneer.py | https://github.com/palantir/python-language-server/blob/master/versioneer.py | MIT |
def render_pep440_post(pieces):
"""TAG[.postDISTANCE[.dev0]+gHEX] .
The ".dev0" means dirty. Note that .dev0 sorts backwards
(a dirty tree will appear "older" than the corresponding clean one),
but you shouldn't be releasing software with -dirty anyways.
Exceptions:
1: no tags. 0.postDISTANCE[... | TAG[.postDISTANCE[.dev0]+gHEX] .
The ".dev0" means dirty. Note that .dev0 sorts backwards
(a dirty tree will appear "older" than the corresponding clean one),
but you shouldn't be releasing software with -dirty anyways.
Exceptions:
1: no tags. 0.postDISTANCE[.dev0]
| render_pep440_post | python | palantir/python-language-server | versioneer.py | https://github.com/palantir/python-language-server/blob/master/versioneer.py | MIT |
def render_pep440_old(pieces):
"""TAG[.postDISTANCE[.dev0]] .
The ".dev0" means dirty.
Eexceptions:
1: no tags. 0.postDISTANCE[.dev0]
"""
if pieces["closest-tag"]:
rendered = pieces["closest-tag"]
if pieces["distance"] or pieces["dirty"]:
rendered += ".post%d" % pie... | TAG[.postDISTANCE[.dev0]] .
The ".dev0" means dirty.
Eexceptions:
1: no tags. 0.postDISTANCE[.dev0]
| render_pep440_old | python | palantir/python-language-server | versioneer.py | https://github.com/palantir/python-language-server/blob/master/versioneer.py | MIT |
def render_git_describe(pieces):
"""TAG[-DISTANCE-gHEX][-dirty].
Like 'git describe --tags --dirty --always'.
Exceptions:
1: no tags. HEX[-dirty] (note: no 'g' prefix)
"""
if pieces["closest-tag"]:
rendered = pieces["closest-tag"]
if pieces["distance"]:
rendered +=... | TAG[-DISTANCE-gHEX][-dirty].
Like 'git describe --tags --dirty --always'.
Exceptions:
1: no tags. HEX[-dirty] (note: no 'g' prefix)
| render_git_describe | python | palantir/python-language-server | versioneer.py | https://github.com/palantir/python-language-server/blob/master/versioneer.py | MIT |
def render_git_describe_long(pieces):
"""TAG-DISTANCE-gHEX[-dirty].
Like 'git describe --tags --dirty --always -long'.
The distance/hash is unconditional.
Exceptions:
1: no tags. HEX[-dirty] (note: no 'g' prefix)
"""
if pieces["closest-tag"]:
rendered = pieces["closest-tag"]
... | TAG-DISTANCE-gHEX[-dirty].
Like 'git describe --tags --dirty --always -long'.
The distance/hash is unconditional.
Exceptions:
1: no tags. HEX[-dirty] (note: no 'g' prefix)
| render_git_describe_long | python | palantir/python-language-server | versioneer.py | https://github.com/palantir/python-language-server/blob/master/versioneer.py | MIT |
def render(pieces, style):
"""Render the given version pieces into the requested style."""
if pieces["error"]:
return {"version": "unknown",
"full-revisionid": pieces.get("long"),
"dirty": None,
"error": pieces["error"],
"date": None}
... | Render the given version pieces into the requested style. | render | python | palantir/python-language-server | versioneer.py | https://github.com/palantir/python-language-server/blob/master/versioneer.py | MIT |
def get_versions(verbose=False):
"""Get the project version from whatever source is available.
Returns dict with two keys: 'version' and 'full'.
"""
if "versioneer" in sys.modules:
# see the discussion in cmdclass.py:get_cmdclass()
del sys.modules["versioneer"]
root = get_root()
... | Get the project version from whatever source is available.
Returns dict with two keys: 'version' and 'full'.
| get_versions | python | palantir/python-language-server | versioneer.py | https://github.com/palantir/python-language-server/blob/master/versioneer.py | MIT |
def get_cmdclass():
"""Get the custom setuptools/distutils subclasses used by Versioneer."""
if "versioneer" in sys.modules:
del sys.modules["versioneer"]
# this fixes the "python setup.py develop" case (also 'install' and
# 'easy_install .'), in which subdependencies of the main project... | Get the custom setuptools/distutils subclasses used by Versioneer. | get_cmdclass | python | palantir/python-language-server | versioneer.py | https://github.com/palantir/python-language-server/blob/master/versioneer.py | MIT |
def do_setup():
"""Main VCS-independent setup function for installing Versioneer."""
root = get_root()
try:
cfg = get_config_from_root(root)
except (EnvironmentError, configparser.NoSectionError,
configparser.NoOptionError) as e:
if isinstance(e, (EnvironmentError, configpars... | Main VCS-independent setup function for installing Versioneer. | do_setup | python | palantir/python-language-server | versioneer.py | https://github.com/palantir/python-language-server/blob/master/versioneer.py | MIT |
def scan_setup_py():
"""Validate the contents of setup.py against Versioneer's expectations."""
found = set()
setters = False
errors = 0
with open("setup.py", "r") as f:
for line in f.readlines():
if "import versioneer" in line:
found.add("import")
if ... | Validate the contents of setup.py against Versioneer's expectations. | scan_setup_py | python | palantir/python-language-server | versioneer.py | https://github.com/palantir/python-language-server/blob/master/versioneer.py | MIT |
def pyls_commands(config, workspace):
"""The list of command strings supported by the server.
Returns:
List[str]: The supported commands.
""" | The list of command strings supported by the server.
Returns:
List[str]: The supported commands.
| pyls_commands | python | palantir/python-language-server | pyls/hookspecs.py | https://github.com/palantir/python-language-server/blob/master/pyls/hookspecs.py | MIT |
def __getitem__(self, item):
"""Override getitem to fallback through multiple dispatchers."""
if self._shutdown and item != 'exit':
# exit is the only allowed method during shutdown
log.debug("Ignoring non-exit method during shutdown: %s", item)
raise KeyError
... | Override getitem to fallback through multiple dispatchers. | __getitem__ | python | palantir/python-language-server | pyls/python_ls.py | https://github.com/palantir/python-language-server/blob/master/pyls/python_ls.py | MIT |
def _hook(self, hook_name, doc_uri=None, **kwargs):
"""Calls hook_name and returns a list of results from all registered handlers"""
workspace = self._match_uri_to_workspace(doc_uri)
doc = workspace.get_document(doc_uri) if doc_uri else None
hook_handlers = self.config.plugin_manager.sub... | Calls hook_name and returns a list of results from all registered handlers | _hook | python | palantir/python-language-server | pyls/python_ls.py | https://github.com/palantir/python-language-server/blob/master/pyls/python_ls.py | MIT |
def urlparse(uri):
"""Parse and decode the parts of a URI."""
scheme, netloc, path, params, query, fragment = parse.urlparse(uri)
return (
parse.unquote(scheme),
parse.unquote(netloc),
parse.unquote(path),
parse.unquote(params),
parse.unquote(query),
parse.unq... | Parse and decode the parts of a URI. | urlparse | python | palantir/python-language-server | pyls/uris.py | https://github.com/palantir/python-language-server/blob/master/pyls/uris.py | MIT |
def urlunparse(parts):
"""Unparse and encode parts of a URI."""
scheme, netloc, path, params, query, fragment = parts
# Avoid encoding the windows drive letter colon
if RE_DRIVE_LETTER_PATH.match(path):
quoted_path = path[:3] + parse.quote(path[3:])
else:
quoted_path = parse.quote(p... | Unparse and encode parts of a URI. | urlunparse | python | palantir/python-language-server | pyls/uris.py | https://github.com/palantir/python-language-server/blob/master/pyls/uris.py | MIT |
def to_fs_path(uri):
"""Returns the filesystem path of the given URI.
Will handle UNC paths and normalize windows drive letters to lower-case. Also
uses the platform specific path separator. Will *not* validate the path for
invalid characters and semantics. Will *not* look at the scheme of this URI.
... | Returns the filesystem path of the given URI.
Will handle UNC paths and normalize windows drive letters to lower-case. Also
uses the platform specific path separator. Will *not* validate the path for
invalid characters and semantics. Will *not* look at the scheme of this URI.
| to_fs_path | python | palantir/python-language-server | pyls/uris.py | https://github.com/palantir/python-language-server/blob/master/pyls/uris.py | MIT |
def from_fs_path(path):
"""Returns a URI for the given filesystem path."""
scheme = 'file'
params, query, fragment = '', '', ''
path, netloc = _normalize_win_path(path)
return urlunparse((scheme, netloc, path, params, query, fragment)) | Returns a URI for the given filesystem path. | from_fs_path | python | palantir/python-language-server | pyls/uris.py | https://github.com/palantir/python-language-server/blob/master/pyls/uris.py | MIT |
def uri_with(uri, scheme=None, netloc=None, path=None, params=None, query=None, fragment=None):
"""Return a URI with the given part(s) replaced.
Parts are decoded / encoded.
"""
old_scheme, old_netloc, old_path, old_params, old_query, old_fragment = urlparse(uri)
path, _netloc = _normalize_win_path... | Return a URI with the given part(s) replaced.
Parts are decoded / encoded.
| uri_with | python | palantir/python-language-server | pyls/uris.py | https://github.com/palantir/python-language-server/blob/master/pyls/uris.py | MIT |
def lock(method):
"""Define an atomic region over a method."""
@functools.wraps(method)
def wrapper(self, *args, **kwargs):
with self._lock:
return method(self, *args, **kwargs)
return wrapper | Define an atomic region over a method. | lock | python | palantir/python-language-server | pyls/workspace.py | https://github.com/palantir/python-language-server/blob/master/pyls/workspace.py | MIT |
def source_roots(self, document_path):
"""Return the source roots for the given document."""
files = _utils.find_parents(self._root_path, document_path, ['setup.py', 'pyproject.toml']) or []
return list({os.path.dirname(project_file) for project_file in files}) or [self._root_path] | Return the source roots for the given document. | source_roots | python | palantir/python-language-server | pyls/workspace.py | https://github.com/palantir/python-language-server/blob/master/pyls/workspace.py | MIT |
def word_at_position(self, position):
"""Get the word under the cursor returning the start and end positions."""
if position['line'] >= len(self.lines):
return ''
line = self.lines[position['line']]
i = position['character']
# Split word in two
start = line[:... | Get the word under the cursor returning the start and end positions. | word_at_position | python | palantir/python-language-server | pyls/workspace.py | https://github.com/palantir/python-language-server/blob/master/pyls/workspace.py | MIT |
def debounce(interval_s, keyed_by=None):
"""Debounce calls to this function until interval_s seconds have passed."""
def wrapper(func):
timers = {}
lock = threading.Lock()
@functools.wraps(func)
def debounced(*args, **kwargs):
call_args = inspect.getcallargs(func, *a... | Debounce calls to this function until interval_s seconds have passed. | debounce | python | palantir/python-language-server | pyls/_utils.py | https://github.com/palantir/python-language-server/blob/master/pyls/_utils.py | MIT |
def find_parents(root, path, names):
"""Find files matching the given names relative to the given path.
Args:
path (str): The file path to start searching up from.
names (List[str]): The file/directory names to look for.
root (str): The directory at which to stop recursing upwards.
... | Find files matching the given names relative to the given path.
Args:
path (str): The file path to start searching up from.
names (List[str]): The file/directory names to look for.
root (str): The directory at which to stop recursing upwards.
Note:
The path MUST be within the r... | find_parents | python | palantir/python-language-server | pyls/_utils.py | https://github.com/palantir/python-language-server/blob/master/pyls/_utils.py | MIT |
def path_to_dot_name(path):
"""Given a path to a module, derive its dot-separated full name."""
directory = os.path.dirname(path)
module_name, _ = os.path.splitext(os.path.basename(path))
full_name = [module_name]
while os.path.exists(os.path.join(directory, '__init__.py')):
this_directory =... | Given a path to a module, derive its dot-separated full name. | path_to_dot_name | python | palantir/python-language-server | pyls/_utils.py | https://github.com/palantir/python-language-server/blob/master/pyls/_utils.py | MIT |
def merge_dicts(dict_a, dict_b):
"""Recursively merge dictionary b into dictionary a.
If override_nones is True, then
"""
def _merge_dicts_(a, b):
for key in set(a.keys()).union(b.keys()):
if key in a and key in b:
if isinstance(a[key], dict) and isinstance(b[key], d... | Recursively merge dictionary b into dictionary a.
If override_nones is True, then
| merge_dicts | python | palantir/python-language-server | pyls/_utils.py | https://github.com/palantir/python-language-server/blob/master/pyls/_utils.py | MIT |
def format_docstring(contents):
"""Python doc strings come in a number of formats, but LSP wants markdown.
Until we can find a fast enough way of discovering and parsing each format,
we can do a little better by at least preserving indentation.
"""
contents = contents.replace('\t', u'\u00A0' * 4)
... | Python doc strings come in a number of formats, but LSP wants markdown.
Until we can find a fast enough way of discovering and parsing each format,
we can do a little better by at least preserving indentation.
| format_docstring | python | palantir/python-language-server | pyls/_utils.py | https://github.com/palantir/python-language-server/blob/master/pyls/_utils.py | MIT |
def clip_column(column, lines, line_number):
"""
Normalise the position as per the LSP that accepts character positions > line length
https://microsoft.github.io/language-server-protocol/specification#position
"""
max_column = len(lines[line_number].rstrip('\r\n')) if len(lines) > line_number else ... |
Normalise the position as per the LSP that accepts character positions > line length
https://microsoft.github.io/language-server-protocol/specification#position
| clip_column | python | palantir/python-language-server | pyls/_utils.py | https://github.com/palantir/python-language-server/blob/master/pyls/_utils.py | MIT |
def position_to_jedi_linecolumn(document, position):
"""
Convert the LSP format 'line', 'character' to Jedi's 'line', 'column'
https://microsoft.github.io/language-server-protocol/specification#position
"""
code_position = {}
if position:
code_position = {'line': position['line'] + 1,
... |
Convert the LSP format 'line', 'character' to Jedi's 'line', 'column'
https://microsoft.github.io/language-server-protocol/specification#position
| position_to_jedi_linecolumn | python | palantir/python-language-server | pyls/_utils.py | https://github.com/palantir/python-language-server/blob/master/pyls/_utils.py | MIT |
def is_process_alive(pid):
"""Check whether the process with the given pid is still alive.
Running `os.kill()` on Windows always exits the process, so it can't be used to check for an alive process.
see: https://docs.python.org/3/library/os.html?highlight=os%20kill#os.kill
Hence ctypes... | Check whether the process with the given pid is still alive.
Running `os.kill()` on Windows always exits the process, so it can't be used to check for an alive process.
see: https://docs.python.org/3/library/os.html?highlight=os%20kill#os.kill
Hence ctypes is used to check for the process dire... | is_process_alive | python | palantir/python-language-server | pyls/_utils.py | https://github.com/palantir/python-language-server/blob/master/pyls/_utils.py | MIT |
def is_process_alive(pid):
"""Check whether the process with the given pid is still alive.
Args:
pid (int): process ID
Returns:
bool: False if the process is not alive or don't have permission to check, True otherwise.
"""
if pid < 0:
return ... | Check whether the process with the given pid is still alive.
Args:
pid (int): process ID
Returns:
bool: False if the process is not alive or don't have permission to check, True otherwise.
| is_process_alive | python | palantir/python-language-server | pyls/_utils.py | https://github.com/palantir/python-language-server/blob/master/pyls/_utils.py | MIT |
def get_keywords():
"""Get the keywords needed to look up the version information."""
# these strings will be replaced by git during git-archive.
# setup.py/versioneer.py will grep for the variable names, so they must
# each be defined on a line of their own. _version.py will just call
# get_keyword... | Get the keywords needed to look up the version information. | get_keywords | python | palantir/python-language-server | pyls/_version.py | https://github.com/palantir/python-language-server/blob/master/pyls/_version.py | MIT |
def get_config():
"""Create, populate and return the VersioneerConfig() object."""
# these strings are filled in when 'setup.py versioneer' creates
# _version.py
cfg = VersioneerConfig()
cfg.VCS = "git"
cfg.style = "pep440"
cfg.tag_prefix = ""
cfg.parentdir_prefix = ""
cfg.versionfil... | Create, populate and return the VersioneerConfig() object. | get_config | python | palantir/python-language-server | pyls/_version.py | https://github.com/palantir/python-language-server/blob/master/pyls/_version.py | MIT |
def get_versions():
"""Get version information or return default if unable to do so."""
# I am in _version.py, which lives at ROOT/VERSIONFILE_SOURCE. If we have
# __file__, we can work backwards from there to the root. Some
# py2exe/bbfreeze/non-CPython implementations don't do __file__, in which
#... | Get version information or return default if unable to do so. | get_versions | python | palantir/python-language-server | pyls/_version.py | https://github.com/palantir/python-language-server/blob/master/pyls/_version.py | MIT |
def _binary_stdio():
"""Construct binary stdio streams (not text mode).
This seems to be different for Window/Unix Python2/3, so going by:
https://stackoverflow.com/questions/2850893/reading-binary-data-from-stdin
"""
PY3K = sys.version_info >= (3, 0)
if PY3K:
# pylint: disable=no-... | Construct binary stdio streams (not text mode).
This seems to be different for Window/Unix Python2/3, so going by:
https://stackoverflow.com/questions/2850893/reading-binary-data-from-stdin
| _binary_stdio | python | palantir/python-language-server | pyls/__main__.py | https://github.com/palantir/python-language-server/blob/master/pyls/__main__.py | MIT |
def settings(self, document_path=None):
"""Settings are constructed from a few sources:
1. User settings, found in user's home directory
2. Plugin settings, reported by PyLS plugins
3. LSP settings, given to us from didChangeConfiguration
4. Project settings, fou... | Settings are constructed from a few sources:
1. User settings, found in user's home directory
2. Plugin settings, reported by PyLS plugins
3. LSP settings, given to us from didChangeConfiguration
4. Project settings, found in config files in the current project.
... | settings | python | palantir/python-language-server | pyls/config/config.py | https://github.com/palantir/python-language-server/blob/master/pyls/config/config.py | MIT |
def update(self, settings):
"""Recursively merge the given settings into the current settings."""
self.settings.cache_clear()
self._settings = settings
log.info("Updated settings to %s", self._settings)
self._update_disabled_plugins() | Recursively merge the given settings into the current settings. | update | python | palantir/python-language-server | pyls/config/config.py | https://github.com/palantir/python-language-server/blob/master/pyls/config/config.py | MIT |
def parse_config(config, key, options):
"""Parse the config with the given options."""
conf = {}
for source, destination, opt_type in options:
opt_value = _get_opt(config, key, source, opt_type)
if opt_value is not None:
_set_opt(conf, destination, opt_val... | Parse the config with the given options. | parse_config | python | palantir/python-language-server | pyls/config/source.py | https://github.com/palantir/python-language-server/blob/master/pyls/config/source.py | MIT |
def _get_opt(config, key, option, opt_type):
"""Get an option from a configparser with the given type."""
for opt_key in [option, option.replace('-', '_')]:
if not config.has_option(key, opt_key):
continue
if opt_type == bool:
return config.getboolean(key, opt_key)
... | Get an option from a configparser with the given type. | _get_opt | python | palantir/python-language-server | pyls/config/source.py | https://github.com/palantir/python-language-server/blob/master/pyls/config/source.py | MIT |
def _set_opt(config_dict, path, value):
"""Set the value in the dictionary at the given path if the value is not None."""
if value is None:
return
if '.' not in path:
config_dict[path] = value
return
key, rest = path.split(".", 1)
if key not in config_dict:
config_d... | Set the value in the dictionary at the given path if the value is not None. | _set_opt | python | palantir/python-language-server | pyls/config/source.py | https://github.com/palantir/python-language-server/blob/master/pyls/config/source.py | MIT |
def run_flake8(flake8_executable, args, document):
"""Run flake8 with the provided arguments, logs errors
from stderr if any.
"""
# a quick temporary fix to deal with Atom
args = [(i if not i.startswith('--ignore=') else FIX_IGNORES_RE.sub('', i))
for i in args if i is not None]
# i... | Run flake8 with the provided arguments, logs errors
from stderr if any.
| run_flake8 | python | palantir/python-language-server | pyls/plugins/flake8_lint.py | https://github.com/palantir/python-language-server/blob/master/pyls/plugins/flake8_lint.py | MIT |
def build_args(options):
"""Build arguments for calling flake8.
Args:
options: dictionary of argument names and their values.
"""
args = ['-'] # use stdin
for arg_name, arg_val in options.items():
if arg_val is None:
continue
arg = None
if isinstance(arg... | Build arguments for calling flake8.
Args:
options: dictionary of argument names and their values.
| build_args | python | palantir/python-language-server | pyls/plugins/flake8_lint.py | https://github.com/palantir/python-language-server/blob/master/pyls/plugins/flake8_lint.py | MIT |
def parse_stdout(document, stdout):
"""
Build a diagnostics from flake8's output, it should extract every result and format
it into a dict that looks like this:
{
'source': 'flake8',
'code': code, # 'E501'
'range': {
'start': {
... |
Build a diagnostics from flake8's output, it should extract every result and format
it into a dict that looks like this:
{
'source': 'flake8',
'code': code, # 'E501'
'range': {
'start': {
'line': start_line,
'ch... | parse_stdout | python | palantir/python-language-server | pyls/plugins/flake8_lint.py | https://github.com/palantir/python-language-server/blob/master/pyls/plugins/flake8_lint.py | MIT |
def pyls_completions(config, document, position):
"""Get formatted completions for current code position"""
settings = config.plugin_settings('jedi_completion', document_path=document.path)
code_position = _utils.position_to_jedi_linecolumn(document, position)
code_position["fuzzy"] = settings.get("fuz... | Get formatted completions for current code position | pyls_completions | python | palantir/python-language-server | pyls/plugins/jedi_completion.py | https://github.com/palantir/python-language-server/blob/master/pyls/plugins/jedi_completion.py | MIT |
def is_exception_class(name):
"""
Determine if a class name is an instance of an Exception.
This returns `False` if the name given corresponds with a instance of
the 'Exception' class, `True` otherwise
"""
try:
return name in [cls.__name__ for cls in Exception.__subclasses__()]
exce... |
Determine if a class name is an instance of an Exception.
This returns `False` if the name given corresponds with a instance of
the 'Exception' class, `True` otherwise
| is_exception_class | python | palantir/python-language-server | pyls/plugins/jedi_completion.py | https://github.com/palantir/python-language-server/blob/master/pyls/plugins/jedi_completion.py | MIT |
def use_snippets(document, position):
"""
Determine if it's necessary to return snippets in code completions.
This returns `False` if a completion is being requested on an import
statement, `True` otherwise.
"""
line = position['line']
lines = document.source.split('\n', line)
act_lines... |
Determine if it's necessary to return snippets in code completions.
This returns `False` if a completion is being requested on an import
statement, `True` otherwise.
| use_snippets | python | palantir/python-language-server | pyls/plugins/jedi_completion.py | https://github.com/palantir/python-language-server/blob/master/pyls/plugins/jedi_completion.py | MIT |
def _sort_text(definition):
""" Ensure builtins appear at the bottom.
Description is of format <type>: <module>.<item>
"""
# If its 'hidden', put it next last
prefix = 'z{}' if definition.name.startswith('_') else 'a{}'
return prefix.format(definition.name) | Ensure builtins appear at the bottom.
Description is of format <type>: <module>.<item>
| _sort_text | python | palantir/python-language-server | pyls/plugins/jedi_completion.py | https://github.com/palantir/python-language-server/blob/master/pyls/plugins/jedi_completion.py | MIT |
def flake(self, message):
""" Get message like <filename>:<lineno>: <msg> """
err_range = {
'start': {'line': message.lineno - 1, 'character': message.col},
'end': {'line': message.lineno - 1, 'character': len(self.lines[message.lineno - 1])},
}
severity = lsp.Di... | Get message like <filename>:<lineno>: <msg> | flake | python | palantir/python-language-server | pyls/plugins/pyflakes_lint.py | https://github.com/palantir/python-language-server/blob/master/pyls/plugins/pyflakes_lint.py | MIT |
def lint(cls, document, is_saved, flags=''):
"""Plugin interface to pyls linter.
Args:
document: The document to be linted.
is_saved: Whether or not the file has been saved to disk.
flags: Additional flags to pass to pylint. Not exposed to
pyls_lint, ... | Plugin interface to pyls linter.
Args:
document: The document to be linted.
is_saved: Whether or not the file has been saved to disk.
flags: Additional flags to pass to pylint. Not exposed to
pyls_lint, but used for testing.
Returns:
A li... | lint | python | palantir/python-language-server | pyls/plugins/pylint_lint.py | https://github.com/palantir/python-language-server/blob/master/pyls/plugins/pylint_lint.py | MIT |
def build_args_stdio(settings):
"""Build arguments for calling pylint.
:param settings: client settings
:type settings: dict
:return: arguments to path to pylint
:rtype: list
"""
pylint_args = settings.get('args')
if pylint_args is None:
return []
return pylint_args | Build arguments for calling pylint.
:param settings: client settings
:type settings: dict
:return: arguments to path to pylint
:rtype: list
| build_args_stdio | python | palantir/python-language-server | pyls/plugins/pylint_lint.py | https://github.com/palantir/python-language-server/blob/master/pyls/plugins/pylint_lint.py | MIT |
def pylint_lint_stdin(pylint_executable, document, flags):
"""Run pylint linter from stdin.
This runs pylint in a subprocess with popen.
This allows passing the file from stdin and as a result
run pylint on unsaved files. Can slowdown the workflow.
:param pylint_executable: path to pylint executab... | Run pylint linter from stdin.
This runs pylint in a subprocess with popen.
This allows passing the file from stdin and as a result
run pylint on unsaved files. Can slowdown the workflow.
:param pylint_executable: path to pylint executable
:type pylint_executable: string
:param document: docume... | pylint_lint_stdin | python | palantir/python-language-server | pyls/plugins/pylint_lint.py | https://github.com/palantir/python-language-server/blob/master/pyls/plugins/pylint_lint.py | MIT |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.