In Mathematica, I create singly linked lists like so:
toLinkedList[x_List] := Fold[pair[#2, #1] &, pair[], Reverse[x]];
fromLinkedList[ll_pair] := List ## Flatten[ll];
emptyQ[pair[]] := True;
emptyQ[_pair] := False;
Using the symbol pair for the cons cells has the advantage of Flatten working safely even if the lists contain Mathematica-style Lists, and allows you to define custom notation using MakeExpression/MakeBoxes, which makes everything much more pleasant. In order to avoid having to muck around with $IterationLimit, I wrote functions to work with these lists using either While loops or NestWhile instead of using recursion. Naturally, I wanted to see which approach would be faster, so I wrote two candidates so I could watch 'em fight:
nestLength[ll_pair] :=
With[{step = {#[[1, -1]], #[[-1]] + 1} &},
Last#NestWhile[step, {ll, 0}, ! emptyQ#First## &]];
whileLength[ll_pair] :=
Module[{result = 0, current = ll},
While[! emptyQ#current,
current = current[[2]];
++result];
result];
The results were very strange. I tested the functions on linked lists of length 10000, and whileLength was usually about 50% faster, at about 0.035 seconds to nestLength's 0.055 seconds. However, occasionally whileLength would take about ~4 seconds. I thought there might be some caching behavior, so I started generating fresh, random lists to check, and whileLength wouldn't necessarily be slow on the first run with a new list; it might take dozens of times to see the slowdown, but then it wouldn't recur (at least not for the 200 runs I was trying with each list).
What might be going on?
For reference, the function I used for testing is this:
getTimes[f_, n_] :=
With[{ll = toLinkedList#RandomInteger[100, 10000]},
Table[Timing[f#ll], {n}][[All, 1]]]
EDIT: I neglected to mention the version earlier; I got these results with Mathematica 8.
EDIT the second: When I read Daniel Lichtblau's answer, I realized that my times for "typical" runs omitted a leading 0. It's been fixed.
EDIT the third: I think Leonid Shifrin is correct to associate the issue with Module; I can get the same sort of behavior from the NestWhile-based version by replacing the With with a Module:
nestModuleLength[ll_pair] :=
Module[{step = {#[[1, -1]], #[[-1]] + 1} &},
Last#NestWhile[step, {ll, 0}, ! emptyQ#First## &]];
In[15]:= Select[getTimes[nestModuleLength, 100], # > 3 &]
Out[15]= {3.797}
The examples below give typical results.
One slow example in a length 20 run.
In[18]:= getTimes[whileLength, 20]
Out[18]= {0.031, 0.032, 0.031, 0.031, 0.031, 0.032, 0.031, 0.031, \
0.031, 0.047, 0.032, 0.031, 0.031, 3.547, 0.047, 0.031, 0.031, 0.032, \
0.031, 0.031}
I note in passing that the timings are ~10x faster than in the original post, except for the slow cases which are comparable. Not sure what accounts for that difference in ratios.
No slow examples.
In[17]:= getTimes[nestLength, 20]
Out[17]= {0.047, 0.047, 0.062, 0.047, 0.047, 0.062, 0.047, 0.047, \
0.047, 0.063, 0.046, 0.047, 0.047, 0.063, 0.047, 0.046, 0.047, 0.063, \
0.047, 0.047}
One slow example in a length 100 run.
In[19]:= getTimes[whileLength, 100]
Out[19]= {0.031, 0.031, 0.031, 0.032, 0.031, 3.594, 0.047, 0.031, \
0.031, 0.031, 0.032, 0.031, 0.031, 0.031, 0.032, 0.031, 0.047, 0.031, \
0.031, 0.031, 0.032, 0.031, 0.031, 0.031, 0.032, 0.047, 0.031, 0.031, \
0.031, 0.032, 0.031, 0.031, 0.031, 0.032, 0.031, 0.031, 0.047, 0.031, \
0.031, 0.032, 0.031, 0.031, 0.031, 0.032, 0.031, 0.031, 0.047, 0.031, \
0.032, 0.031, 0.031, 0.031, 0.032, 0.031, 0.031, 0.047, 0.031, 0.031, \
0.032, 0.031, 0.031, 0.031, 0.032, 0.031, 0.047, 0.031, 0.031, 0.032, \
0.031, 0.031, 0.031, 0.032, 0.031, 0.031, 0.031, 0.032, 0.046, 0.032, \
0.031, 0.031, 0.031, 0.032, 0.031, 0.031, 0.047, 0.031, 0.032, 0.031, \
0.031, 0.031, 0.032, 0.031, 0.047, 0.031, 0.031, 0.031, 0.032, 0.031, \
0.031, 0.031}
Mathematica implements, imperfectly, what is called "infinite evaluation". That is to say, an expression reevaluates until it stops changing. To make this reasonably fast there are various optimizations that attempt to short circuit the process whenever possible.
In some cases this can be tricky to discern (due to an effect similar to hash collisions), and expressions might be needlessly reevaluated. Deeply nested expressions tend to be a worst case for this. We have further code that will often address these even in cases of collisions.
The culprit in this instance is exactly this code that attempts to determine quickly whether an expression requires reevaluation. It is peculiar but possibly a clue (to someone) that this happens at most once in a run inside that While loop. So something happens in the bad cases that prevents recurrence whilst inside the same While.
At one time I was familiar with the reevaluation detection code, having written a chunk of it. But it was rewritten for version 8. So even after seeing this suboptimal behavior in a debugger, it is is something of a mystery to me. About all I can say right now is that I filed a bug report.
As Leonid Shifrin observed, symbols with Attribute of HoldAllComplete are immune to this problem. So using that attribute can be beneficial for this type of code.
Daniel Lichtblau
Wolfram Research
A disclaimer: the following is a speculation. This seems to be related to the search for UpValues. It looks like this has been optimized for global variables (so that the system skips this step when it can determine that it can do that), but not for Module - generated local variables. To test this, assign HoldAllComplete attribute to pair, and the effect disappears (since then, UpValues are not checked for current):
SetAttributes[pair, HoldAllComplete];
In[17]:= ll = toLinkedList#RandomInteger[100, 10000];
Max[Table[Timing[whileLength[ll]], {1000}][[All, 1]]]
Out[18]= 0.047
HTH
Seems it's related to the Module local symbols memory management.
I'll show the timing series from some runs. Each run gives of course a unique Plot, but I checked "consistency" among runs. Look:
whileLength[l2_pair] :=
Module[{result = 0}, current = l2;
While[! emptyQ#current, current = current[[2]];
++result];
result];
gives the following Timing series:
While using only Global symbols:
whileLength[l2_pair] :=
Module[{}, result = 0; current = l2;
While[! emptyQ#current, current = current[[2]];
++result];
result];
gives:
Related
This code takes 10 seconds to compile to a pdf. If I enable in the code the additional lines it takes 2 minutes to compile for the 5 lines. And this happens everytime.
Why does it take so long, and how can I prevent it?
The system is texlive on windows with latest packages:
This is LuaHBTeX, Version 1.15.0 (TeX Live 2022) (format=lualatex 2022.12.10) 17 DEC 2022 20:53
restricted system commands enabled.
LaTeX2e <2022-11-01> patch level 1
Lua module: luaotfload 2022-10-03 3.23 Lua based OpenType font support
This is the code
% !TeX encoding=utf8
% !TeX program = lualatex
\documentclass{article}
\usepackage{fontspec}
\usepackage{amsmath}
\usepackage{unicode-math}
\usepackage{tabularray}
\usepackage{xcolor}
\begin{document}
\newcommand{\fontstring}{Sphinx of black quartz, judge my vow.}
\newcommand{\mathstring}{$f(u,v) = \iiint \left[u\nabla^{2}v+\left(\nabla u,\nabla v\right)\right]\mathrm{d}^{3}V$}
\colorlet{tablerowcolor}{gray!10}
\colorlet{tablesubheadcolor}{azure3!30}
{ % start a group
\small\renewcommand{\arraystretch}{1.4}\sffamily
% the table
\begin{longtblr}[
caption = {Font examples},
label = {tab:doc:Font:Gallery}]
{
colspec = {X[1,l]>{\ttfamily}X[2,l]},
width = 1.0\textwidth,
row{odd} = {bg=tablerowcolor},
row{1} = {bg=azure3, fg=white, font=\sffamily\upshape},
rowhead = 1,
rowfoot = 0,
}
\hline % Table Header
Font & Example \\
\hline %
%
\SetCell[c=2]{l,bg=tablesubheadcolor} Latin Modern Family \\
Latin Modern Roman & \setmainfont{Latin Modern Roman} \rmfamily \fontstring \\
Latin Modern Sans & \setsansfont{Latin Modern Sans} \sffamily\fontstring \\
Latin Modern Mono & \setmonofont{Latin Modern Mono} \ttfamily \fontstring \\
%
\SetCell[c=2]{l,bg=tablesubheadcolor} Math Fonts \\
%
Latin Modern Math & \setmathfont{Latin Modern Math} \rmfamily \mathstring \\
% TeX Gyre Termes Math & \setmathfont{TeX Gyre Termes Math} \rmfamily \mathstring \\
% TeX Gyre Pagella Math & \setmathfont{TeX Gyre Pagella Math} \rmfamily \mathstring \\
% Charter - math version & \setmathfont{XCharter-Math.otf} \rmfamily \mathstring \\
% Garamond math version & \setmathfont{Garamond-Math} \rmfamily \mathstring \\
% Cambria Math & \setmathfont{Cambria Math} \rmfamily \mathstring \\
% %
\hline
%
\end{longtblr}
} % close the group
\end{document}
Setup: Win 10, 2x Geforce RTX 2080 Ti, Tensorflow 2.9.1 (also tested older versions), Geforce Driver 512.95
I tried multiple tutorials for multi GPU training with Tensorflow 2 and was never able to utelize more then one GPU.
Here is my code.
def mnist_dataset(batch_size):
(x_train, y_train), _ = tf.keras.datasets.mnist.load_data()
x_train = x_train / np.float32(255)
y_train = y_train.astype(np.int64)
train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train)).shuffle(60000).repeat().batch(batch_size)
return train_dataset
def build_and_compile_cnn_model():
model = tf.keras.Sequential([
tf.keras.layers.InputLayer(input_shape=(28, 28)),
tf.keras.layers.Reshape(target_shape=(28, 28, 1)),
tf.keras.layers.Conv2D(512, 3, activation='relu'),
tf.keras.layers.Conv2D(512, 3, activation='relu'),
tf.keras.layers.Conv2D(512, 3, activation='relu'),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(10)
])
model.compile(
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
optimizer=tf.keras.optimizers.Adam(),
metrics=['accuracy'])
model.summary()
return model
strategy = tf.distribute.MirroredStrategy()
#strategy = tf.distribute.MirroredStrategy(devices=["/gpu:0", "/gpu:1"], cross_device_ops=tf.distribute.ReductionToOneDevice())
print(tf.__version__)
print(tf.config.list_physical_devices())
print('Number of devices: {}'.format(strategy.num_replicas_in_sync))
with strategy.scope():
multi_worker_model = build_and_compile_cnn_model()
batch_size = 64
multi_worker_dataset = mnist_dataset(batch_size)
multi_worker_model = build_and_compile_cnn_model()
multi_worker_model.fit(multi_worker_dataset, epochs=10, steps_per_epoch=250)
Output from tf.config.list_physical_devices() and strategy.num_replicas_in_sync.
[PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU'), PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU'), PhysicalDevice(name='/physical_device:GPU:1', device_type='GPU')]
Number of devices: 2
But only one GPU gets used.
GPU usage and temperature multi GPU test
To test if both GPUs are working and are accessable by Tensorflow I tried this code.
with tf.device('/gpu:0'):
batch_size = 64
single_worker_dataset = mnist_dataset(batch_size)
single_worker_model = build_and_compile_cnn_model()
single_worker_model.fit(single_worker_dataset, epochs=5, steps_per_epoch=250)
with tf.device('/gpu:1'):
batch_size = 64
single_worker_dataset = mnist_dataset(batch_size)
single_worker_model = build_and_compile_cnn_model()
single_worker_model.fit(single_worker_dataset, epochs=5, steps_per_epoch=250)
Runs as expected. First GPU:0 gets used and then GPU:1. No Error. Everything is fine.
GPU usage and temperature single GPU test
In Tensorflow 1 I was able to use multiple GPUs with keras.utils multi_gpu_model and the same setup.
Any idea what the problem could be?
I feel confused when using the Roberta tokenizer in Huggingface.
>>> tokenizer = RobertaTokenizer.from_pretrained('roberta-base')
>>> x = tokenizer.tokenize("The tiger is ___ (big) than the dog.")
['The', 'Ġtiger', 'Ġis', 'Ġ___', 'Ġ(', 'big', ')', 'Ġthan', 'Ġthe', 'Ġdog', '.']
>>> x = tokenizer.tokenize("The tiger is ___ ( big ) than the dog.")
['The', 'Ġtiger', 'Ġis', 'Ġ___', 'Ġ(', 'Ġbig', 'Ġ)', 'Ġthan', 'Ġthe', 'Ġdog', '.']
>>> x = tokenizer.encode("The tiger is ___ (big) than the dog.")
[0, 20, 23921, 16, 2165, 36, 8527, 43, 87, 5, 2335, 4, 2]
>>> x = tokenizer.encode("The tiger is ___ ( big ) than the dog.")
[0, 20, 23921, 16, 2165, 36, 380, 4839, 87, 5, 2335, 4, 2]
>>>
Question: (big) and ( big ) have different tokenization results, which result in different token id as well. Which one I should use? Does it mean that I should pre-tokenize the input first to make it ( big ) and go for RobertaTokenization? Or it doesn't really matter?
Secondly, it seems BertTokenizer has no such confusion:
>>> tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
>>> x = tokenizer.tokenize("The tiger is ___ (big) than the dog.")
['the', 'tiger', 'is', '_', '_', '_', '(', 'big', ')', 'than', 'the', 'dog', '.']
>>> x = tokenizer.tokenize("The tiger is ___ ( big ) than the dog.")
['the', 'tiger', 'is', '_', '_', '_', '(', 'big', ')', 'than', 'the', 'dog', '.']
>>>
BertTokenizer gives me the same results using the wordpieces.
Any thoughts to help me better understand the RobertaTokenizer, which I know is using Byte-Pair Encoding?
Hugingface's Transformers are designed such that you are not supposed to do any pre-tokenization.
RoBERTa uses SentecePiece which has lossless pre-tokenization. I.e., when you have a tokenized text, you should always be able to say how the text looked like before tokenization. The Ġ (which is ▁, a weird Unicode underscore in the original SentecePiece) says that there should be a space when you detokenize. As a consequence big and ▁big end up as different tokens. Of course, in this particular context, it does not make much sense because it is obviously still the same word, but this the price you pay for lossless tokenization and also how RoBERTa was trained.
BERT uses WordPiece, which does not suffer from this problem. On the other hand, the mapping between the original string and the tokenized text is not as straightforward (which might be inconvenient, e.g., when you want to highlight something in a user-generated text).
I want to use snakemake for making a bioinformatics pipeline and I googled it and read documents and other stuff, but I still don't know how to get it works.
Here are some of my raw data files.
Rawdata/010_0_bua_1.fq.gz, Rawdata/010_0_bua_2.fq.gz
Rawdata/11_15_ap_1.fq.gz, Rawdata/11_15_ap_2.fq.gz
...they are all paired files.)
Here is my align.snakemake
from os.path import join
STAR_INDEX = "/app/ref/ensembl/human/idx/"
SAMPLE_DIR = "Rawdata"
SAMPLES, = glob_wildcards(SAMPLE_DIR + "/{sample}_1.fq.gz")
R1 = '{sample}_1.fq.gz'
R2 = '{sample}_2.fq.gz'
rule alignment:
input:
r1 = join(SAMPLE_DIR, R1),
r2 = join(SAMPLE_DIR, R2),
params:
STAR_INDEX = STAR_INDEX
output:
"Align/{sample}.bam"
message:
"--- mapping STAR---"
shell:"""
mkdir -p Align/{wildcards.sample}
STAR --genomeDir {params.STAR_INDEX} --readFilesCommand zcat --readFilesIn {input.r1} {input.r2} --outFileNamePrefix Align/{wildcards.sample}/log
"""
This is it. I run this file by "snakemake -np -s align.snakemake" and I got this error.
WorkflowError:
Target rules may not contain wildcards. Please specify concrete files or a rule without wildcards.
I am sorry that I ask this question, there are many people using it pretty well though. any help would be really appriciated. Sorry for my English.
P.S. I read the official document and tutorial but still have no idea.
Oh I did. Here is my answer to my question for some people might want some help.
from os.path import join
STAR_INDEX = "/app/ref/ensembl/human/idx/"
SAMPLE_DIR = "Rawdata"
SAMPLES, = glob_wildcards(SAMPLE_DIR + "/{sample}_1.fq.gz")
R1 = '{sample}_1.fq.gz'
R2 = '{sample}_2.fq.gz'
rule all:
input:
expand("Align/{sample}/Aligned.toTranscriptome.out.bam", sample=SAMPLES)
rule alignment:
input:
r1 = join(SAMPLE_DIR, R1),
r2 = join(SAMPLE_DIR, R2)
params:
STAR_INDEX = STAR_INDEX
output:
"Align/{sample}/Aligned.toTranscriptome.out.bam"
threads:
8
message:
"--- Mapping STAR---"
shell:"""
mkdir -p Align/{wildcards.sample}
STAR --genomeDir {params.STAR_INDEX} --outSAMunmapped Within --outFilterType BySJout --outSAMattributes NH HI AS NM MD --outFilterMultimapNmax 20 --outFilterMismatchNmax 999 --outFilterMismatchNoverLmax 0.04 --alignIntronMin 20 --alignIntronMax 1000000 --alignMatesGapMax 1000000 --alignSJoverhangMin 8 --alignSJDBoverhangMin 1 --sjdbScore 1 --runThreadN {threads} --genomeLoad NoSharedMemory --outSAMtype BAM Unsorted --quantMode TranscriptomeSAM --outSAMheaderHD \#HD VN:1.4 SO:unsorted --readFilesCommand zcat --readFilesIn {input.r1} {input.r2} --outFileNamePrefix Align/{wildcards.sample}/log
"""
I want to print text/characters in Hindi/Sanskrit language.Can anybody guide me using Ruby language how can I achieve this? Are there any libraries/gems available to achieve this? I tried searching for this but could not find desired resources.Basically various webistes displays its contents in Hindi, Gujarati, Sanskrit etc languages but I guess that rendering task is done by browser using font files.Using a programming language how we can achieve the same?
Thanks.
In ruby we have [ ].pack('U*') method which converts the given array's value(as Unicode values) to corresponding characters and "XYZ".unpack('U*') which returns me the Unicode values of each characters.
After executing,
[2309].pack('U*')
I got अ
Let's understand it with an example,
I want मिरो में आपके लिए एक नया सिलाई आदेश
Then execute
"मिरो में आपके लिए एक नया सिलाई आदेश".unpack('U*')
This will return me Unicode values of each character of above string.
[32, 2350, 2375, 2306, 32, 2310, 2346, 2325, 2375, 32, 2354, 2367, 2319, 32, 2319, 2325, 32, 2344, 2351, 2366, 32, 2360, 2367, 2354, 2366, 2312, 32, 2310, 2342, 2375, 2358]
Then to regenerate the above Hindi string,
[2350, 2367, 2352, 2379, 32, 2350, 2375, 2306, 32, 2310, 2346, 2325, 2375, 32, 2354, 2367, 2319, 32, 2319, 2325, 32, 2344, 2351, 2366, 32, 2360, 2367, 2354, 2366, 2312, 32, 2310, 2342, 2375, 2358].pack('U*')
Note: .unpack('U*') return's unicode since we asked for it by mentioning 'U*', This unpack/pack methods are also works for binary and many other forms.
Read more about pack/unpack
It's nout about the server-side language - it's about using the proper character encoding for your data. UTF-8 is the most common format used to support international languages. Most of India is covered. The browser does all the work and no font additions are required (unless you're getting really fancy with the typography).
उदाहरण के लिये
See: http://www.unicode.org/standard/supported.html
You need to use the right encoding, which for interoperability, is UTF-8. To use UTF-8 in your program you need to add the following comment at the top of your file:
#encoding: utf-8
Then, you need to use your text editors way of inserting symbols in in Hindi or other languages into strings, and Ruby will happily print them.